0

我正在使用以下代码在我的 iOS 应用程序的 touchesEnded 处截取绘图:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

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

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

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

    CGSize size = activeView.frame.size;

    NSUInteger width = size.width * 2;
    NSUInteger height = size.height * 2;

    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();

    unsigned char *rawData = malloc(height * width * 4);
    memset(rawData,0,height * width * 4);

    NSUInteger bytesPerPixel = 4;
    NSUInteger bytesPerRow = bytesPerPixel * width;
    NSUInteger bitsPerComponent = 8;
    CGContextRef context = CGBitmapContextCreate(rawData, width, height, bitsPerComponent, bytesPerRow, colorSpace, kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);

    CGRect bounds;
    bounds.origin = CGPointMake(0,0);
    bounds.size = size;

    CGContextScaleCTM(context, 2.0, 2.0);

    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), [self.topimage CGImage]);
    CGContextDrawImage(context, CGRectMake(0, 0, size.width, size.height), [bottomimage CGImage]);

    CGImageRef imageRef2 = CGBitmapContextCreateImage(context);
    UIImage *latest2 = [UIImage imageWithCGImage:imageRef2 scale:0.0 orientation:UIImageOrientationDownMirrored];

    activeView.layer.contentsScale = 2.0;
    [activeView.layer renderInContext:context];

    CGImageRef imageRef = CGBitmapContextCreateImage(context);
    UIImage *latest = [UIImage imageWithCGImage:imageRef scale:0.0 orientation:UIImageOrientationDownMirrored];

    CGContextRelease(context);
    CGColorSpaceRelease(colorSpace);
    CGImageRelease(imageRef);
    CGImageRelease(imageRef2);
    free(rawData);

    [_pages replaceObjectAtIndex:_sidebar.selectedIndex withObject:latest];

    [_sidebar reloadData];

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

    if([_pagessavedbg objectAtIndex:selectedIndex])
    [_pagessavedbg replaceObjectAtIndex:selectedIndex withObject:latest2];

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

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


});

但是,每次调用此代码(在 touchesEnded 时),内存使用量都会增加大约 10MB。为什么会这样?有没有更好的替代方法可以使用更少的内存?

4

1 回答 1

3

正如贾斯汀所说,2048 * 1536 * 4 = 12MB。这个事实是数学,无法避免。但是,如果分配 12MB 是一个问题,那么有很多方法可以管理您的内存利用率(对于短期使用来说,1GB 设备上的内存并不是那么多,所以您应该询问您要解决的问题,但无论如何。 ...)

  • 如果您经常这样做,请不要重新分配内存。重复使用它。这样,您只需支付一次。
  • 如果您不需要 2048*1536*4 的图像,请创建一个较小的位图并缩放您的图像以适应它。
  • 分配较小的位图并将部分图像渲染到其中。稍后将它们缝合在一起。

但是您是否有目标内存分配?虽然视网膜显示器一直是许多应用程序的主要内存问题,但短暂的 12MB 分配通常不是问题。更多时候是所需的时间,而不是内存。

但是,如果您必须有一个 2048*1536*4 的位图,它将占用 12MB 的内存。

于 2012-10-31T02:10:20.753 回答