如何在应用程序中存储多个图像对象而不会收到内存警告

时间:2014-05-06 11:48:51

标签: ios memory-management

我将我的图像对象存储在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{
            }
        });
    });
}

1 个答案:

答案 0 :(得分:1)

将图像写入磁盘并将文件路径存储到阵列中的图像。