我使用ASIHTTPRequest将多个图片上传到服务器。
-(void)uploadImagesToServer
{
[[self networkQueue] cancelAllOperations];
// Creating a new queue each time we use it means we don't have to worry about clearing delegates or resetting progress tracking
[self setNetworkQueue:[ASINetworkQueue queue]];
[[self networkQueue] setDelegate:self];
[[self networkQueue] setRequestDidStartSelector:@selector(uploadRequestStarted:)];
[[self networkQueue] setRequestDidFinishSelector:@selector(requestFinished:)];
[[self networkQueue] setRequestDidFailSelector:@selector(requestFailed:)];
[[self networkQueue] setQueueDidFinishSelector:@selector(queueFinished:)];
[[self networkQueue] setUploadProgressDelegate:self.uploadBar];
[[self networkQueue] setShowAccurateProgress:YES];
[ASIHTTPRequest setDefaultTimeOutSeconds:30];
for (int i = 0; i< appDelegate.selImageDetails.count; i++) {
NSMutableDictionary *dic = [appDelegate.selImageDetails objectAtIndex:i];
NSString *orderid = [[NSUserDefaults standardUserDefaults] objectForKey:@"orderId"];
if ([[dic objectForKey:@"Status"] isEqualToString:@"N"] || [[dic objectForKey:@"Status"] isEqualToString:@"Failed"])
{
NSURL *url = [NSURL URLWithString:@"http://180.151.100.53:9776/App_Frame_IT/UploadImage"];
NSLog(@"UPLOAD IMAGE ARRAY == %@",dic);
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc]init];
ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
[request addPostValue:orderid forKey:@"OrderId"];
[request addPostValue:[dic objectForKey:@"ImageName"] forKey:@"imgName"];
[request addPostValue:@"10" forKey:@"appId"];
[request addPostValue:[dic objectForKey:@"Frame"] forKey:@"frameSize"];
[request addPostValue:[dic objectForKey:@"Quantity"] forKey:@"Quantity"];
NSString *imgCount = [NSString stringWithFormat:@"%d", [appDelegate.selImageDetails count]];
[request addPostValue:imgCount forKey:@"totalimages"];
[request addPostValue:[dic objectForKey:@"paperQuality"] forKey:@"paper_quality"];
NSURL *assetURL = [dic objectForKey:@"assetPathURL"];
ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:assetURL resultBlock:^(ALAsset *asset) {
ALAssetRepresentation *rep = [asset defaultRepresentation];
Byte *buffer = (Byte*)malloc(rep.size);
NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];//this is NSData may be what you want
//NSLog(@"returned Image ====> %@", data);
// UIImageView *testImageView = [[UIImageView alloc]initWithFrame:CGRectMake(50, 10, 50, 50)];
NSLog(@"FUll REsolution Image Size in MB = %.2f",(float)data.length/1024.0f/1024.0f);
//[data writeToFile:photoFile atomically:YES];//you can save image later
//NSData *imageData = [[NSData alloc] initWithContentsOfMappedFile:[dic objectForKey:@"Path"]];
[request addData:data withFileName:[dic objectForKey:@"ImageName"] andContentType:@"image/png" forKey:@"uploadfile"];
//[data release];
NSLog(@"requestGenrated table count---%d ",[appDelegate.selImageDetails count]);
NSString *requestUserName = [NSString stringWithFormat:@"upload%i",i];
[request setUsername:requestUserName];
[request setUserInfo:[NSDictionary dictionaryWithObject:[dic objectForKey:@"unique"] forKey:@"uniqueId"]];
[request setTag:i];
[request setShowAccurateProgress:YES];
[request setRequestMethod:@"POST"];
[request setShouldContinueWhenAppEntersBackground:YES];
[[self networkQueue] addOperation:request];
[dic setObject:@"Y" forKey:@"requestGenrated"];
[appDelegate.selImageDetails replaceObjectAtIndex:i withObject:dic];
NSLog(@"Upload Request Created");
} failureBlock:^(NSError *err) {
NSLog(@"Error: %@",[err localizedDescription]);
}];
[pool drain];
}
else
{
NSLog(@"((Image already uploaded))");
}
}
failedRequests = 0;
[networkQueue setShouldCancelAllRequestsOnFailure:YES];
[[self networkQueue] go];
}
我从代码中可以看到来自ALAssetsLibrary
的这些图片。我能够成功上传所有图像,但问题是,它消耗了太多内存(见截图)
即使在上传所有图像或移动到不同视图后,我也无法释放此内存。我想做什么,我要发布哪个对象?
由于
答案 0 :(得分:0)
设置cachePolicy
的{{1}}以避免缓存,在NSURLRequest
request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData