iphone - CGBitmapContextCreate 的内存使用率非常高

标签 iphone objective-c ios xcode memory-management

我正在使用以下代码在我的 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。为什么会这样?有没有更好的替代方法可以使用很多更少的内存?

最佳答案

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

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

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

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

关于iphone - CGBitmapContextCreate 的内存使用率非常高,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13147875/

相关文章:

objective-c - 将图像发送到 AirPrint 的函数

iphone - UIProgressView 未在 iPhone 上显示,但在模拟器中显示

iOS:通过电子邮件的 Firebase 邀请不起作用

ios - CSS 样式根据设备奇怪地呈现

iphone - 向 CGMutableRefPath 添加边框颜色和圆角

iphone - 加载了 "PersonnalDetails" Nib 但没有获得 UITableView

iphone - 如何防止触摸空格键时键盘从数字变为字母?

iphone - 通过 initWithFrame 添加 subview 。 UINavigationBar 大小

Objective-C:具有 Split View的 UITabBarController

ios - 尽管不使用自动布局,但垂直滚动在 UIScrollView 和 iOS7 和 Xcode 5 中不起作用