【问题标题】:How to store multiple image object in app without getting memory warning如何在应用程序中存储多个图像对象而不会收到内存警告
【发布时间】:2014-06-23 00:29:50
【问题描述】:

我将我的图像对象存储在 NSMutableArray 中,但是在 11 个图像之后我收到内存警告, 有没有比这更有效的方法而无需物理存储图像。

我只想在我的应用程序中保留该对象,直到用户单击按钮并且我正在存储图像。

更新

我在内部存储图像。但是现在当我在一个请求中将所有图像(即 22 张)上传到服务器时,应用程序崩溃并显示消息“应用程序因内存压力而崩溃”

-(void) uploadImages:(NSArray* )imageAry
{

    __block NSString * tmpdocumentType =documentType;
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.mycompany.myqueue", 0);

    dispatch_async(backgroundQueue, ^{

        tmpdocumentType = [tmpdocumentType stringByAddingPercentEscapesUsingEncoding:
                           NSASCIIStringEncoding];
        NSString *urlString = [NSString stringWithFormat:@"http://myserver.com/wsname.aspx"];

        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:3600.f];
        [request setHTTPMethod:@"POST"];

        NSString *boundary = @"---------------------------14737809831466499882746641449";
        NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
        [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

        NSMutableData *body = [NSMutableData data];

        for (int i=0; i<imageAry.count; i++) {
            [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"images[]\"; filename=\"test%i.png\"\r\n",i] dataUsingEncoding:NSUTF8StringEncoding]];
            [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];

            UIImage * image = [self loadImage:[imageAry objectAtIndex:i]];

            [body appendData:UIImageJPEGRepresentation(image, 1.0)];
            [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
            image = nil;
        }

        [body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];

        [request setHTTPBody:body];

        NSError * error = Nil;

        NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:&error];

        NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

        dispatch_async(dispatch_get_main_queue(), ^{
            if([returnString rangeOfString:@"Success"].location!=NSNotFound){
            }else{
            }
        });
    });
}

【问题讨论】:

  • 把你的图片保存在本地,然后保存所有图片的路径,不要放在数组中,因为图片大小会导致内存问题

标签: ios memory-management


【解决方案1】:

将图像写入磁盘并将图像的文件路径存储在数组中。

【讨论】:

  • 上传时内部存储仍然应用崩溃
  • 不要一次将所有图像保存在内存中。在极端情况下,您在任何给定时间内只能在内存中保存一张图像。
  • 但由于我的应用程序需要,我必须在 1 个请求中上传所有图像。比我应该怎么做。我不能一一上传。我应该添加我的代码。
  • 简单回答:这是不可能的。您不能随您的应用程序提供额外的内存。您是否尝试压缩图像?
  • 实际上我希望该图像在服务器上具有完整的高度和宽度,因此我无法压缩它。 +1 回复。
猜你喜欢
  • 2012-04-08
  • 1970-01-01
  • 1970-01-01
  • 2012-01-16
  • 1970-01-01
  • 2013-10-31
  • 1970-01-01
  • 1970-01-01
  • 2011-06-06
相关资源
最近更新 更多