【发布时间】:2012-12-24 23:21:44
【问题描述】:
我正在尝试将图像从 iPhone 上传到网络服务,但即使文件成功上传,也无法查看 jpg,它似乎已损坏。
使用此 c# 代码可以成功上传文件并正常工作:
var url = http://myurl.co.uk/services/service.svc/UploadImage?id=108&ext=png;
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
req.Method = "POST";
req.ContentType = "image/jpeg";
using (var reqStream = req.GetRequestStream())
{
var bytes = File.ReadAllBytes("C:\\Users\\Chris\\Pictures\\default.png”);
reqStream.Write(bytes, 0, bytes.Length);
}
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
Console.WriteLine("HTTP/{0} {1} {2}", resp.ProtocolVersion, (int)resp.StatusCode, resp.StatusDescription);
但这是我在 iOS 上尝试的使文件不可见的代码:
获取图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Resize the image from the camera
UIImage *scaledImage = [image resizedImageWithContentMode:UIViewContentModeScaleAspectFill bounds:CGSizeMake(_photo.frame.size.width/2, _photo.frame.size.height/2) interpolationQuality:kCGInterpolationHigh];
// Crop the image to a square (yikes, fancy!)
UIImage *croppedImage = [scaledImage croppedImage:CGRectMake((scaledImage.size.width -_photo.frame.size.width)/2, (scaledImage.size.height -_photo.frame.size.height)/2, _photo.frame.size.width, _photo.frame.size.height)];
// Show the photo on the screen
_photo.image = croppedImage;
NSData *imgData=UIImageJPEGRepresentation(croppedImage,1);
NSLog(@"Original size:%d",[imgData length]);
NSData *smallerImage=UIImageJPEGRepresentation(croppedImage, 0.5);
NSLog(@"End size:%d",[smallerImage length]);
imageData = imgData;
NSString *imageName = @"tempImage.jpg";
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectoryPath = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectoryPath stringByAppendingPathComponent:imageName];
NSData* settingsData = UIImageJPEGRepresentation(croppedImage, 0.5);
[settingsData writeToFile:dataPath atomically:YES];
[picker dismissModalViewControllerAnimated:YES];
}
上传图片:
-(void)uploadImageForOfferID:(NSString *)offerID imageExtention:(NSString *)extension withCompletionBlock:(void(^)(NSError *error))block
{
NSString *imageName = @"tempImage.jpg";
NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsDirectoryPath = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectoryPath stringByAppendingPathComponent:imageName];
NSData* imageData = [NSData dataWithContentsOfFile:dataPath];
AFHTTPClient *client = [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:masterURL]];
NSString *url = [NSString stringWithFormat:@"UploadImage?id=%@&ext=%@", offerID, extension];
NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:url parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
[formData appendPartWithFileData:imageData name:@"photo" fileName:@"photo.jpg" mimeType:@"image/jpeg"];
}];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
}];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Complete");
block(nil);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failed");
NSLog(@"FAILED WITH STATUS CODE %d - error description: %@", operation.response.statusCode, error.description);
block(error);
}];
[operation start];
}
此方法先将图像保存到文件,然后再上传。我也尝试从NSData *smallerImage=UIImageJPEGRepresentation(croppedImage, 0.5); 上传数据,但结果相同。
我在上传时遗漏了什么吗?
谢谢
编辑----
比较模拟器中的文件和上传的文件。模拟器文件为2,538 bytes (4 KB on disk),上传的文件稍大2,698 bytes (4 KB on disk)
原始模拟器图像显示缩略图,但上传的图像没有!
上传的图片可以用 Photoshop 打开,但不能用 Safari 或 Chrome 打开。
【问题讨论】:
-
将 iDevice 上保存的图像(使用模拟器更容易做到)与服务器上的图像进行比较,Web Server 接受它但数据已损坏是不寻常的。如果没有区别,请稍等片刻,然后在不同的浏览器中打开图像。我之前在 Mac 上使用 Chrome 时也遇到过类似的问题。图片似乎已损坏,并且由于某些缓存原因没有刷新,但在 Firefox 中看起来很好。
-
请参阅问题底部的编辑。比较的文件略有不同。
-
这可能无关紧要,但是您的 c# 正在获取 png 文件并将其保存为 jpeg。服务是否期待这种变化?
var bytes = File.ReadAllBytes("C:\\Users\\Chris\\Pictures\\default.png”);看起来你在 iOS 中的临时图像是 jpg。 -
我也注意到了。如果显示它的 Web 服务有效,则编写者提供 c# 代码。 Web 服务设置为使用 &ext=png 或 &ext=jpg URL 字段同时接受 png 和 jpg
-
模拟器镜像是
Original size:4223,而AFNetworking发送的字节是Sent 4382 of 4382 bytes
标签: iphone ios web-services afnetworking