我正在尝试上传iphone的图片,从相机胶卷或新的,我使用此代码获取图像 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)信息
self.image = UIImagePNGRepresentation([info objectForKey:UIImagePickerControllerEditedImage]);
我使用此
将图像发送到服务器-(void)subeFoto1{
NSString *urlString = [NSString stringWithFormat:@"http://bonsai.com.ec/scrum/SubeImagen.php"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"img.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:self.image]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
self.urlImage = [NSString stringWithFormat:@"http://bonsai.com.ec/scrum/%@", returnString];
NSLog(@"URL %@", self.urlImage);
}
但这是永远的!我改变了PHP所以现在我保存的图像是250kb,但问题是在我认为的应用程序,任何想法?
答案 0 :(得分:1)
这里有一些问题:
以下是如何使用MKNetworkKit上传图像的示例:
NSData *imageData = UIImageJPEGRepresentation(self.image, 0.85f);
MKNetworkOperation *op = [[MKNetworkOperation alloc] initWithURLString:@"http://bonsai.com.ec/scrum/SubeImagen.php"
params:nil
httpMethod:@"POST"];
[op addData:imageData forKey:@"uploadedfile" mimeType:@"image/jpeg" fileName:@"img.jpg"];
[op addCompletionHandler:^(MKNetworkOperation *completedOperation) {
// this will be called when the request succeeds
} errorHandler:^(MKNetworkOperation *completedOperation, NSError *error) {
// this will be called if the request fails
}];
[[NSOperationQueue mainQueue] addOperation:op];
有一点需要注意的是,由于这是一次异步操作,您的程序不会停止!上传将在幕后进行。操作完成后,将在以后的某个时刻调用完成处理程序或错误处理程序。