Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8707813
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:57:38+00:00 2026-06-13T03:57:38+00:00

I am calling the following code on touchesEnded in my drawing application. However, every

  • 0

I am calling the following code on touchesEnded in my drawing application. However, every time this is called, the memory usage goes up by about 2MB. Eventually it can be around 90-100MB and then the app will crash, if the user draws quickly.

What am I doing wrong here? How can I make this code speedy but with minimal memory usage?

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

    if(_pagessavedbg.count > selectedIndex)
        self.topimage = [[UIImage alloc] initWithCGImage:[[_pagessavedbg objectAtIndex:selectedIndex] CGImage]];

    else
        self.topimage = [[UIImage alloc] initWithCGImage:[[UIImage imageNamed:@"BlankImage.png"] CGImage]];

    UIImage *bottomimage = [notification.userInfo objectForKey:@"Image"];

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

    NSError *error2 = nil;

    NSURL *documentsURL = [[NSFileManager defaultManager]
                           URLForDirectory:NSDocumentDirectory
                           inDomain:NSUserDomainMask
                           appropriateForURL:nil
                           create:YES
                           error:nil];

    CGSize size = CGSizeMake(874, 732);
    UIGraphicsBeginImageContextWithOptions(size, NO, 1);
    [self.topimage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    [bottomimage drawInRect:CGRectMake(0, 0, size.width, size.height)];
    UIImage *latest = UIGraphicsGetImageFromCurrentImageContext();
    [activeView.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *latest2 = UIGraphicsGetImageFromCurrentImageContext();
    CGContextFlush(UIGraphicsGetCurrentContext());
    UIGraphicsEndImageContext();

    if(_pages.count > selectedIndex)
    {
    if([_pages objectAtIndex:selectedIndex])
    [_pages replaceObjectAtIndex:selectedIndex withObject:latest];
    }

    if(_pages.count > selectedIndex)
    {
    if([_pagessavedbg objectAtIndex:selectedIndex])
    [_pagessavedbg replaceObjectAtIndex:selectedIndex withObject:latest2];

    else
    [_pagessavedbg insertObject:latest2 atIndex:selectedIndex];
    }

    NSData *data = UIImageJPEGRepresentation(latest, 1.0);

    NSError *error = nil;

    NSDictionary *fileObjectMap = @{
    @"ImageData" : data,
    };

    for (NSString *filename in fileObjectMap)
    {
        NSData       *data = [NSPropertyListSerialization
                              dataWithPropertyList:fileObjectMap[filename]
                              format:NSPropertyListXMLFormat_v1_0
                              options:0
                              error:&error];

        if (!data) {
            NSLog(@"Failed to serialize array for filename %@ (contents %@) with error %@", filename, fileObjectMap[filename], error);
            return;
        }

        NSURL        *fileURL = [[documentsURL URLByAppendingPathComponent:filename]
                                 URLByAppendingPathExtension:@"txt"];

        if (![data writeToURL:fileURL options:NSDataWritingAtomic error:&error]) {
            NSLog(@"Failed to write data to file URL %@ for filename %@ (data %@) with error %@", fileURL, filename, data, error);
            return;
        }
    }

    NSString *indexnotext = [defaults objectForKey:@"IndexNumber"];
    myInt = [indexnotext intValue];

    dispatch_async(dispatch_get_main_queue(), ^{
    [self.sidebar reloadData];
    [self.sidebar scrollRowAtIndexToVisible:_sidebar.selectedIndex];
    });    

    if (!documentsURL) {
        NSLog(@"Failed to get documents URL: %@", error2);
        return;
    }

    NSMutableArray *imageDatas = [NSMutableArray arrayWithCapacity:self.pages.count];

    for (UIImage *image in [_pages copy])
    {

        if(!image)
        break;

        else
        [imageDatas addObject:UIImageJPEGRepresentation(image, 1.0)];

    }

    NSMutableArray *imageDatas2 = [NSMutableArray arrayWithCapacity:self.pagessavedbg.count];

    for (UIImage *image2 in [self.pagessavedbg copy])
    {
        if(!image2)
        break;

        else
        [imageDatas2 addObject:UIImageJPEGRepresentation(image2, 1.0)];

    }

    NSDictionary *fileObjectMap2 = @{
    [NSString stringWithFormat:@"SavedPages%i", [_indexnumber intValue]] : imageDatas,
    [NSString stringWithFormat:@"SavedPagesBG%i", [_indexnumber intValue]] : imageDatas2,
    };

    for (NSString *filename2 in fileObjectMap2)
    {
        self.data2 = [NSPropertyListSerialization
                              dataWithPropertyList:fileObjectMap2[filename2]
                              format:NSPropertyListXMLFormat_v1_0
                              options:0
                              error:&error2];

        if (!self.data2) {
            NSLog(@"Failed to serialize array for filename %@ (contents %@) with error %@", filename2, fileObjectMap2[filename2], error2);
            return;
        }

        NSURL        *fileURL2 = [[documentsURL URLByAppendingPathComponent:filename2]
                                 URLByAppendingPathExtension:@"txt"];

        if (![self.data2 writeToURL:fileURL2 options:NSDataWritingAtomic error:&error2]) {
            NSLog(@"Failed to write data to file URL %@ for filename %@ (data %@) with error %@", fileURL2, filename2, self.data2, error2);
            return;
        }

    }

});
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:57:39+00:00Added an answer on June 13, 2026 at 3:57 am

    Use the Instrument Tool For XCODE

    1. Click Product -> Profile
    2. Click the Leaks
    3. Check the Link
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am calling a jQuery plugin every 5 seconds with the following code var
I am calling the following code two times in my application. Will Android start
Using the following code I'm unable to get value of this.select inside the calling
I have the following code (calling a method returning IEnumerable): FibonacciWithLinq.Skip(delegate() { return 5;
The following code adds a checkbox and a label to the calling node. My
If many threads are calling GetNextNumber simultaneously with the following code, GetNextNumber will return
Following is my code with the business logic. I get below json by calling
I'm calling a java program from my python code in the following way: subprocess.check_output([java,
I publish my web application using msbuild by calling following command: msbuild myweb.csproj /T:Package
I am calling Lucene using the following code (PyLucene, to be precise): analyzer =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.