在后台上传时损坏的图像

时间:2012-11-13 15:43:06

标签: ios image asihttprequest

我正在为我的公司开发一个小图像共享应用程序。一切都很好,除了一件事:当我将图像上传到服务器,并将应用程序切换到背景时,图像的一部分已损坏(全灰色)。

只要应用程序处于活动状态,似乎图像数据就会正确发送。一旦我切换到背景,它就不会发送任何内容。

对于记录,我使用ASIHttpRequest,shouldContinueWhenAppEntersBackground设置为YES,我正在运行从iOS 4.3到iOS 6.0的应用程序。我正在使用ARC。

我试图“保留”(通过强引用)图像和数据,也没有。

以下是代码的一部分:

在 发送图像的Web服务

- (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类的实际部分

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];

如果你们中的任何一个人有最简单的想法,我会接受它! 感谢。

1 个答案:

答案 0 :(得分:0)

最后我决定使用不同的库(MKNetworkKit)而不是发送UIImage,我将磁盘上的图像保存到tmp文件夹并发送文件。下载完成后,我只删除磁盘上的图像。它的作品很有魅力:)