我一直在寻找新AFNetworking 2.0上传图片的示例。但我上传的图片总是失败。所以这是我使用的代码
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *imagetes = info[UIImagePickerControllerOriginalImage];
self.picprofile.image = imagetes;
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *result = [PHAsset fetchAssetsWithALAssetURLs:@[refURL] options:nil];
NSString *filename = [[result firstObject] filename];
NSLog(@"FileName == %@", filename);
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{@"do":@"profile",@"what":@"editFoto",@"session":sesion};
NSString *URLString = BaseURLString;
NSData *imageData = UIImageJPEGRepresentation(self.picprofile.image, 0.5); // image size ca. 50 KB
NSLog(@"imageData == %@", imageData);
[manager.requestSerializer setTimeoutInterval:120];
manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObject:@"text/html"];
[manager POST:URLString parameters:parameters constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"foto" fileName:filename mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success %@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure %@, %@", error, operation.responseString);
}];
[self dismissViewControllerAnimated:NO completion:nil];
}