ios - 仅由于内存错误,iOS8 上的应用程序崩溃

标签 ios objective-c ipad memory ios8

我正在尝试生成图表并一张一张地截取它的屏幕截图。该应用程序一次正常工作,然后当我第二次尝试再次捕获屏幕截图时,应用程序崩溃说应用程序因内存错误而终止。这仅发生在 iOS8 中,而不发生在 iOS7 中。这里是捕获屏幕截图的代码,它在线崩溃 - [view.layer renderInContext:context]; 该应用程序在峰值使用时使用 124 MB,并且在崩溃之前没有生成内存警告。即使没有其他应用程序在后台运行,应用程序在 iOS 8 上也会崩溃。如果我注释掉上面的行,应用程序根本不会崩溃,但截取的屏幕截图不是完整的屏幕。

- (BOOL)captureView:(UIView *)view forGraph:(NSString *)graphName
{

BOOL isImageCpatured = NO;

@try {
    CGRect rect = view.bounds;
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [view.layer renderInContext:context];
    UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    CGImageRef imageRef;

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 113, 1024, 532));
    }
    else {
        imageRef = CGImageCreateWithImageInRect([img CGImage], CGRectMake(0, 93, 1024, 532));
    }

    img = nil;

    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);        
    NSData *pngData = UIImagePNGRepresentation(image);

    [pngData writeToFile:[Utility_Class documentsPathForFileName:[NSString stringWithFormat:@"%@BG.png",graphName]] atomically:YES];

    pngData = nil;

    image = nil;

    isImageCpatured = YES;

}
@catch (NSException *exception) {
    return isImageCpatured;
}
@finally {

}

return isImageCpatured;
}

可能出了什么问题?我找不到任何解决方案。我发现内存压力与内存错误不同。但没有解决方案如何解决它。甚至在 @autorelease 池中调用 captureView 函数。

这是在 iOS8 上生成的未知崩溃日志。 iOS7它永远不会崩溃。

Incident Identifier: 24F29058-7D47-40B6-87B4-2183220DB55B
CrashReporter Key:   6079e634bf7aeebb0e4d9ea95336cb33b0fc49dd
Hardware Model:      iPad4,1
OS Version:          iPhone OS 8.0 (12A365)
Kernel Version:      Darwin Kernel Version 14.0.0: Tue Aug 19 15:09:47 PDT 2014; root:xnu-2783.1.72~8/RELEASE_ARM64_S5L8960X
Date:                2014-09-25 18:21:25 +0530
Time since snapshot: 88 ms
Free pages:                              2219
Active pages:                            96945
Inactive pages:                          48102
Speculative pages:                       314
Throttled pages:                         0
Purgeable pages:                         2
Wired pages:                             77304
File-backed pages:                       40758
Anonymous pages:                         104603
Compressions:                            1467794
Decompressions:                          158957
Compressor Size:                         24980
Uncompressed Pages in Compressor:        119797
Page Size:                               16384
Largest process: my application Name

最佳答案

这是由于 CGImageCreateWithImageInRect 的错误造成的。见:http://openradar.appspot.com/radar?id=5780787475513344

来源:https://twitter.com/steipete/status/524224268952158208

关于ios - 仅由于内存错误,iOS8 上的应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26039332/

相关文章:

objective-c - 防止 NSTextField 留空

ipad - Monotouch 应用程序在 iPad 上使用更多内存?

iphone - UITabBar 上的 adBannerView

ios - 一种使用 UIPinchGestureRecognizer UIRotationGestureRecognizer 和 UIPanGestureRecognizer 添加 Instagram 类布局指南的方法?

ios - Admob 不加载 iOS 10 iMessage 应用程序扩展的广告

ios - 将 block 形式的 objective-c 解决为 swift 3

ios - UINavigationItem BackButton 显示与 UINavigationItem 相同吗?

iphone - 如何从 iphone 中的 nsmutablearray 中删除所选行?

iOS 我应该为大文本使用 UILabel

ios - 是什么导致具有相同自动布局约束的两个 UITableViewCells 表现不同?