Upload image from iOS to PHP

前端 未结 1 1229
耶瑟儿~
耶瑟儿~ 2020-12-20 09:18

I\'m trying to upload an image from my iOS App to my web server via PHP. Here is the following code:

-(void)uploadImage {
    NSData *imageData = UIImageJPEG         


        
相关标签:
1条回答
  • 2020-12-20 09:54

    Finally I used the AFNetworking library to handle this. As I haven't found clear methods to do this on the web and on stackoverflow, here is my answer to easily post user's images from their iOS device to your server via PHP. Majority of the code comes from this stackoverflow post.

    -(void)uploadImage { 
        image = [self scaleImage:image toSize:CGSizeMake(800, 800)];
        NSData *imageData = UIImageJPEGRepresentation(image, 0.7);
    
        // 1. Create `AFHTTPRequestSerializer` which will create your request.
        AFHTTPRequestSerializer *serializer = [AFHTTPRequestSerializer serializer];
    
        NSDictionary *parameters = @{@"your_param": @"param_value"};
    
        NSError *__autoreleasing* error;
        // 2. Create an `NSMutableURLRequest`.
        NSMutableURLRequest *request = [serializer multipartFormRequestWithMethod:@"POST" URLString:@"http://www.yoururl.com/script.php" parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
            [formData appendPartWithFileData:imageData
                                        name:@"userfile"
                                    fileName:@"image.jpg"
                                    mimeType:@"image/jpg"];
        } error:(NSError *__autoreleasing *)error];
        // 3. Create and use `AFHTTPRequestOperationManager` to create an `AFHTTPRequestOperation` from the `NSMutableURLRequest` that we just created.
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        AFHTTPRequestOperation *operation =
        [manager HTTPRequestOperationWithRequest:request
                                         success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                             NSLog(@"Success %@", responseObject);
                                         } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                             NSLog(@"Failure %@", error.description);
                                         }];
    
        // 4. Set the progress block of the operation.
        [operation setUploadProgressBlock:^(NSUInteger __unused bytesWritten,
                                            long long totalBytesWritten,
                                            long long totalBytesExpectedToWrite) {
            //NSLog(@"Wrote %lld/%lld", totalBytesWritten, totalBytesExpectedToWrite);
            [self.progressBarView setProgress:(double)totalBytesWritten / (double)totalBytesExpectedToWrite animated:YES];
        }];
    
        // 5. Begin!
        operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
    
        self.progressView.hidden = NO;
        [operation start];
    }
    

    I think it will help new xcoders.

    Cheers.

    0 讨论(0)
提交回复
热议问题