0

touchesEnded在我的绘图应用程序中调用以下代码。但是,每次调用它时,内存使用量都会增加大约 2MB。最终它可能在 90-100MB 左右,如果用户快速绘制,应用程序将崩溃。

我在这里做错了什么?我怎样才能使这段代码快速但内存使用最少?

    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;
        }

    }

});
4

1 回答 1

-1

使用 XCODE 仪器工具

  1. 点击产品 -> 简介
  2. 单击泄漏
  3. 检查链接
于 2012-10-20T11:39:46.323 回答