在后台上传时损坏的图像 [英] Corrupted image when uploading in background
问题描述
我正在为我的公司开发一个小图像共享应用程序。一切都很好,除了一件事:当我将图像上传到服务器,并将应用程序切换到背景时,图像的一部分已损坏(全灰色)。
I'm developping a little image sharing app for my company. Everything works pretty fine except for one thing : when I upload an image to the server, and switch the app to background, a part of the image is corrupted (all gray).
只要应用程序处于活动状态,图像数据似乎就会正确发送。一旦我切换到后台,它就不会发送任何内容。
It seems that the image data is sent correctly as long as the app is live. As soon as I switch to background, it sends nothing as it seems.
对于记录,我使用ASIHttpRequest,shouldContinueWhenAppEntersBackground设置为YES并且我正在运行从iOS 4.3到iOS 6.0的应用程序。我正在使用ARC。
For the record, I use ASIHttpRequest, the shouldContinueWhenAppEntersBackground is set to YES and I'm running the app from iOS 4.3 to iOS 6.0. I'm using ARC.
我试图保留(通过强引用)图像和数据,也没有。
I tried to "retain" (through a strong reference) both the image and the data, nothing there too.
以下是代码的一部分:
发送图像的
Web服务
The Webservice that sends the image
- (void)sendImage:(UIImage*)image forEmail:(NSString*)email
{
NSString* uploadImage = [NSString stringWithFormat:[self completeUrlForService:SEND_PHOTO], email];
NSURL* url = [NSURL URLWithString:[uploadImage stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
NSLog(@"uploadImage %@", uploadImage);
// setting that we will send a JSON object
[self setRequestType:WebServiceWrapperTypePOSTRequest];
// when posting a picture, it could take more time...
self.request.timeOutSeconds = 4*60;
[self.request setShouldContinueWhenAppEntersBackground:YES];
// setting up the POST data
[self addPostData:image forKey:@"fileContents"];
// start the request
[self startRequestForUrl:url userInfo:[NSDictionary dictionaryWithObject:SEND_PHOTO forKey:URL_KEY]];
}
ASIHttpRequest类的实际部分
the actual part of ASIHttpRequest class
self.request = [ASIHTTPRequest requestWithURL:url];
[self.request setShouldContinueWhenAppEntersBackground:YES];
NSString* key = [[self.postDictionnary allKeys] objectAtIndex:0];
UIImage* value = [self.postDictionnary valueForKey:key];
__strong NSData* data = UIImageJPEGRepresentation(value, 1.0);
if (!data) data = UIImagePNGRepresentation(value);
[self.request appendPostData:data];
[self.request setPostLength:data.length];
[self.request setUserInfo:userInfo];
[self.request setDelegate:self];
[self.request startAsynchronous];
如果你们中的任何一个人有最简单的想法,我会接受它!
谢谢。
If any of you guys has the tinyest idea, I'll take it! Thanks.
推荐答案
最后我决定使用不同的库(MKNetworkKit),而不是发送UIImage,我将磁盘上的映像保存到tmp文件夹并改为发送文件。下载完成后,我只删除磁盘上的图像。它很有魅力:)
Finally I decided to use a different library (MKNetworkKit) and instead of sending an UIImage, I save the image on disk to the tmp folder and send the file instead. When the download is complete, I just delete the image on disk. It worked liked a charm :)
这篇关于在后台上传时损坏的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!