IOS 代码片段 (Update!)

1、分享到Facebook连接。
http://m.facebook.com/sharer.php?u=URL&t=标题


2、移除ABPeoplePickerNavigationController右边的Cancel按钮
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.delegate = self;
.................

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {
    // Here we want to remove the 'Cancel' button, but only if we're showing
    // either of the ABPeoplePickerNavigationController's top two controllers
    viewController.navigationItem.rightBarButtonItem = nil;
}

摘自: http://stackoverflow.com/questions/1611499/abpeoplepickernavigationcontroller-remove-cancel-button-without-using-privat

3.ios multipart request related code:
- (void) uploadPicture{
    
    UIImage *img = [UIImage imageNamed:@"icon.png"];
    NSData *imageData = UIImageJPEGRepresentation(img, 90);
    //Here i tried 2 ways of getting the data for uploading, but both don't work.
    
    // create the URL
    NSURL *postURL = [NSURL URLWithString:@"http://*********/PictureUpload"];
    
    // create the connection
    NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:postURL cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20.0];
    
    // change type to POST (default is GET)
    [postRequest setHTTPMethod:@"POST"];
    
    // just some random text that will never occur in the body
    NSString *stringBoundary = @"0xKhTmLbOuNdArY---This_Is_ThE_BoUnDaRyy---pqo";
    
    // header value, user session ID added
    NSString *headerBoundary = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",
                                stringBoundary];
    
    // set header
    [postRequest addValue:headerBoundary forHTTPHeaderField:@"Content-Type"];
    // create data
    NSMutableData *postBody = [NSMutableData data];
    
    // text part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"title\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"hello" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    
    // media/image part
    [postBody appendData:[[NSString stringWithFormat:@"--%@\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file\"; filename=\"%@.jpeg\"\r\n", @"helloImage"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[@"Content-Type: image/jpeg\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    [postBody appendData:[NSData dataWithData:imageData]];
    [postBody appendData:[@"Content-Transfer-Encoding: binary\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
    
    // final boundary
    [postBody appendData:[[NSString stringWithFormat:@"--%@--\r\n", stringBoundary] dataUsingEncoding:NSUTF8StringEncoding]];
    NSString *s = [[NSString alloc] initWithData:postBody encoding:NSASCIIStringEncoding];
    NSLog(@"%@", s);
    
    // add body to post
    [postRequest setHTTPBody:postBody];
    
    // pointers to some necessary objects
    NSURLResponse* response;
    NSError* error;
    
    // synchronous filling of data from HTTP POST response
    NSData *responseData = [NSURLConnection sendSynchronousRequest:postRequest returningResponse:&response error:&error];
    
    [NSURLConnection sendAsynchronousRequest:postRequest queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
        if (error)
        {
            NSLog(@"Error: %@", [error localizedDescription]);
        }
        
        // convert data into string
        NSString *responseString = [[NSString alloc] initWithBytes:[responseData bytes]
                                                            length:[responseData length]
                                                          encoding:NSUTF8StringEncoding];
        
        // see if we get a welcome result
        NSLog(@"%@", responseString);
    }];
}

See http://stackoverflow.com/questions/8561403/multipart-formdata-image-upload-get-the-file
See https://gist.github.com/1354221
See http://en.wikipedia.org/wiki/MIME

4.Popup 效果弹出视图
CGAffineTransform transform = CGAffineTransformIdentity;
    [self.view setTransform:CGAffineTransformScale(transform, 0.3, 0.3)];
    
    [UIView animateWithDuration:0.2 animations:^{
        [self.view setTransform:CGAffineTransformScale(transform, 1.1, 1.1)];
    } completion:^(BOOL finished) {
        [UIView animateWithDuration:0.13 animations:^{
            [self.view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 0.9, 0.9)];
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.13 animations:^{
                [self.view setTransform:CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0)];
            } completion:^(BOOL finished) {
                //do nothing
            }];
        }];
    }];


5.上下摇晃动画效果
CALayer*viewLayer=[self.inputHolderView layer];
        CABasicAnimation*animation=[CABasicAnimation animationWithKeyPath:@"transform"];
        animation.duration=0.1;
        animation.repeatCount = 4;
        animation.autoreverses=YES;
        animation.fromValue=[NSValue valueWithCATransform3D:CATransform3DRotate(viewLayer.transform, -0.03, 0.0, 0.0, 0.05)];
        animation.toValue=[NSValue valueWithCATransform3D:CATransform3DRotate(viewLayer.transform, 0.03, 0.0, 0.0, 0.05)];
        [viewLayer addAnimation:animation forKey:@"wiggle"];


6.检查ARC的宏
#if !__has_feature(objc_arc)
    [result autorelease];
#endif

猜你喜欢

转载自re-reference.iteye.com/blog/1661579
今日推荐