我正在尝试将图片/视频上传到适用于iOS的网络服务器。 这个服务器的上传部分工作正常。我使用Android版本检查了它,我已经在Android应用中实现了上传方法。 所以我在stackoverflow.com上找到了一些iOS代码 首先,我使用以下代码上传图像。 但是我无法上传并获得以下结果。我在iOS8 SDK上使用XCode6.1。
Error Domain=NSURLErrorDomain Code=-1001 "The request timed out." UserInfo=0x7fe24348d0b0 {NSUnderlyingError=0x7fe2434be120 "The request timed out.", NSErrorFailingURLStringKey=ServerURL, NSErrorFailingURLKey=ServerURL, NSLocalizedDescription=The request timed out.}
以下是我正在使用的代码。
NSString* serverURL = @"http://www.myserver.com/file/postMedia.php";
UIImage *image = [UIImage imageNamed:@"sample.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSDictionary *param = @{@"userID":@"master",
[manager POST:serverURL parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"uploadedfile_thumb" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
....
});
return;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
....
});
当然,服务器工作正常。 我已经使用Android代码进行了测试。 所以我想知道iOS的确切代码。
谢谢
答案 0 :(得分:0)
这是我使用AFNetworking将图像上传到服务器的工作代码:
+ (void)uploadImage:(UIImage *)image
ForUser:(WECUser *)user
withSuccessBlock:(void (^)(NSDictionary *response))resultBlock
faliureBlock:(void (^)(NSError *error))faliureBlock {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
formatter.dateFormat = @"yyyyMMddHHmmss";
NSString *dateString = [formatter stringFromDate:[NSDate date]];
NSString *fileName =
[NSString stringWithFormat:@"user_image_%@.jpg", dateString];
NSDictionary *dictionary = @{
@"user_id" : user._id,
@"resource_id" : user._id,
@"functioncode" : @"2000",
@"app_id" : [WECURLGenerator stringOfAppID],
@"file_name" : fileName
};
NSURLRequest *request = [[AFHTTPRequestSerializer serializer]
multipartFormRequestWithMethod:
@"POST" URLString:[WECURLGenerator stringOfImageUploadingBaseURL]
parameters:dictionary
constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData
appendPartWithFileData:UIImageJPEGRepresentation(image, 0.6)
name:@"file1"
fileName:fileName
mimeType:@"jpg"];
} error:(NULL)];
AFHTTPRequestOperation *operation =
[[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation,
id responseObject) {
NSDictionary *resultDict =
[NSJSONSerialization JSONObjectWithData:responseObject
options:NSJSONReadingMutableLeaves
error:nil];
resultBlock(resultDict[@"record"]);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
faliureBlock(error);
}];
[operation start];
}
希望这有帮助。
答案 1 :(得分:0)
您的代码看起来很好,我使用相同的代码使用AFNetworking上传图像。但是你还没有提到你如何选择图像。因为您无法直接上传图片,如:
UIImage *image = [UIImage imageNamed:@"sample.png"];
您是否正在使用UIImagepickerController来挑选图片?
如果是,请参阅我的代码;
-(IBAction)chooseImg:(id)sender{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
picker = [[UIImagePickerController alloc]init];
picker.allowsEditing = NO;
picker.delegate = self;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
[self.navigationController presentViewController:picker animated:YES completion:nil];
}
else{
picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.allowsEditing = YES;
picker.delegate = self;
picker.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self.navigationController presentViewController:picker animated:YES completion:nil];
}
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSURL *name = [info objectForKey:UIImagePickerControllerReferenceURL];
image = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibraryAssetForURLResultBlock resultBlock = ^(ALAsset *imageAsset){
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSDictionary *metadata = imageAsset.defaultRepresentation.metadata;
NSLog(@"Meta:%@",metadata);
NSString *str = [imageRep filename];
txtImg.text = str;
};
ALAssetsLibrary *assetLib = [[ALAssetsLibrary alloc]init];
[assetLib assetForURL:name resultBlock:resultBlock failureBlock:nil];
[self.navigationController dismissViewControllerAnimated:YES completion:nil];
}
注意:这里我使用元数据来显示图像的名称,没有必要跳过这个。
比上传部分:
NSString* serverURL = @"http://www.myserver.com/file/postMedia.php";
//UIImage *image = [UIImage imageNamed:@"sample.png"];
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSDictionary *param = @{@"userID":@"master",
[manager POST:serverURL parameters:param constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"uploadedfile_thumb" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
....
});
return;
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
....
});
这适用于我上传图片。 希望对你有效。