0

我正在使用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正如您在上面的代码中看到的那样,我正在获取这些图像。我能够成功上传所有图片,但问题是,它消耗了太多内存(见截图)

在此处输入图像描述

即使上传所有图像或移动到不同的视图后,我也无法释放此内存。我想做什么,我必须释放哪个对象?

谢谢

4

1 回答 1

0

设置的cachePolicy避免NSURLRequest缓存,应该类似ASIFormDataRequest

request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData

于 2013-10-15T02:17:44.977 回答