我的程序是写的,用于从凸轮上传图片,示例代码如下:
#define WEBSERVICE_URL @"http://192.168.0.104/upload.php"
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissViewControllerAnimated:YES completion:^{
UIImage *selectedImage = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(selectedImage);
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:WEBSERVICE_URL parameters:nil constructingBodyWithBlock:^(id formData) {
[formData appendPartWithFileData:imageData name:@"upfile" fileName:@"test" mimeType:@"image/png"];
} error:nil];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSProgress *progress = nil;
NSURLSessionUploadTask *uploadTask = [manager uploadTaskWithStreamedRequest:request progress:&progress completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
[progress removeObserver:self forKeyPath:@"fractionCompleted"];
NSLog(error.debugDescription);
if (error) {
[self.view updateWithMessage:[NSString stringWithFormat:@"Error : %@!", error.debugDescription]];
} else {
[self.view updateWithMessage:@"Great success!"];
}
}];
[progress addObserver:self forKeyPath:@"fractionCompleted" options:NSKeyValueObservingOptionNew context:NULL];
[uploadTask resume];
self.imageUploadProgress = [[TNSexyImageUploadProgress alloc] init];
self.imageUploadProgress.radius = 100;
self.imageUploadProgress.progressBorderThickness = -10;
self.imageUploadProgress.trackColor = [UIColor blackColor];
self.imageUploadProgress.progressColor = [UIColor whiteColor];
self.imageUploadProgress.imageToUpload = selectedImage;
[self.imageUploadProgress show];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(imageUploadCompleted:) name:IMAGE_UPLOAD_COMPLETED object:self.imageUploadProgress];
}];
}
错误是:
TNSexyImageUploadProgressDemo[5275:113032] Error Domain=NSURLErrorDomain Code=-1001 "The operation couldn’t be completed. (NSURLErrorDomain error -1001.)" UserInfo=0x7f92c2d907d0 {NSErrorFailingURLStringKey="
http://192.168.0.104/upload.php
", NSUnderlyingError=0x7f92c2dcd5c0 "The operation couldn’t be completed. (kCFErrorDomainCFNetwork error -1001.)", NSErrorFailingURLKey="http://192.168.0.104/upload.php
"}
无论在模拟器或iPhone上,同样的错误,我使用safari访问网址,一切都是正确的,我使用wifi访问网络。 ping 192.168.0.104没问题。
服务器的程序是用php编写的,代码流失:
http://192.168.0.104/upload.php
答案 0 :(得分:2)
自从我开始使用Xcode 6以来,我遇到了同样的问题。更新AFNetworking库解决了这个问题。一切都恢复正常。
https://github.com/AFNetworking/AFNetworking
如果您正在使用Xcode 6,请尝试一下。