将多个图像上传到服务器时出现内存问题

Posted

技术标签:

【中文标题】将多个图像上传到服务器时出现内存问题【英文标题】:Memory issue while uploading multiple images to server 【发布时间】:2013-08-23 07:11:36 【问题描述】:

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

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

谢谢

【问题讨论】:

您的项目是否启用了 ARC? 不,我尝试转换它,但困难太多。 @TheDoctor 你的问题解决了吗? @Garry:不,我没有。 【参考方案1】:

设置NSURLRequestcachePolicy避免缓存,应该和ASIFormDataRequest类似

request.cachePolicy = NSURLRequestReloadIgnoringLocalCacheData

【讨论】:

以上是关于将多个图像上传到服务器时出现内存问题的主要内容,如果未能解决你的问题,请参考以下文章

尝试将文件(图像)上传到服务器时出现错误 403

从 iphone 设备上传图像时出现图像方向问题

在 iOS 中上传时出现 NSOperationQueue(ASINetworkQueue) 问题

上传图片到服务器时出现绿线

上传图片到服务器时出现绿线

上传到 Firebase 时出现图片方向问题