0

I am storing my image object in the NSMutableArray but after 11 image i get the memory warning, is there any more efficient way than this without storing image physically.

I just want to hold that object in my app until user click the button and at time i am storing the images.

UPDATE

I am storing images internally. But now when i upload this all image(which is 22) on server in one request app crashes With message "App crashes because of Memory Pressure"

-(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{
            }
        });
    });
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • save ur images locally then save the path for all images dont put in array it will cause the memory issue because of image size – amit soni May 06 '14 at 11:51

1 Answers1

1

Write the images to disk and store a file path to the image in the array.

dasdom
  • 13,975
  • 2
  • 47
  • 58
  • Stored internally still app crashe on upload –  May 06 '14 at 13:27
  • Don't hold all images at once in memory. In the extreme case you can only hold one image in memory in any given time. – dasdom May 06 '14 at 13:47
  • But as my app required i have to upload all image in 1 request. Than what should i do. I cant upload it one by one. Should i add my code. –  May 06 '14 at 13:55
  • Simple answer: This is not possible. You can't ship additional memory with your App. Did you try to compress the images? – dasdom May 06 '14 at 13:57
  • Actually i want that image in its full height and width on server so i cant compress it. +1 for response. –  May 06 '14 at 13:59
  • Your requirements are false. There are not made with the platform in mind. The only way out I can think of is to change the requirements to fit the platform. – dasdom May 06 '14 at 14:01
  • Hou much MB can i hold in the Memory. i means how much MB image i can upload in one request. And by the way thanx for response i am accepting answer –  May 06 '14 at 14:07
  • http://stackoverflow.com/questions/6044147/memory-limit-and-ios-memory-allocation-in-iphone-sdk – dasdom May 06 '14 at 14:11