OGeek|极客世界-中国程序员成长平台

标题: iphone - 什么可能导致 [UIImage imageWithData] 返回时崩溃 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-11 20:53
标题: iphone - 什么可能导致 [UIImage imageWithData] 返回时崩溃

我的 NSManagedObjects 之一中有此代码:

if (self.tempImageStorage) {
    return self.tempImageStorage;
} else if(self.imageData) {
    NSLog(@"%@ %d",self.imageData, self.imageData.length);
    self.tempImageStorage = [UIImage imageWithData:self.imageData];
    return self.tempImageStorage;
}

有时,通常当我快速浏览图像时,它会在第 5 行(UIImage imageWithData 行)返回 EXC_BAD_ACCESS,我无法 po (printobject) self在控制台中,所以我假设 NSManagedObject 本身已被释放。没有意义的是它能够为 2 个 if 引用 self,我什至在它之前记录了图像数据及其长度。

NSManagedObject 如何在这段时间内被释放,因为这一切都发生在同一个线程上(而且它是主线程)?

PS:我启用了 Zombies,但它仍然不能让我 po self。

设置图像数据的代码:

- (void) processRequestASIHTTPRequest *) request
{
    UIImage * orig = [UIImage imageWithData:[request responseData]];
    CGSize targetSize = isPad()?CGSizeMake(446,150):CGSizeMake(320, 108);
    CGRect fourPanelSize = CGRectMake(0, 0, 1000, 336);
    CGImageRef imageRef = CGImageCreateWithImageInRect([orig CGImage], fourPanelSize);
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone || bitmapInfo == kCGImageAlphaLast) {
        bitmapInfo = kCGImageAlphaPremultipliedLast;
    }

    CGContextRef bitmap;
    bitmap = CGBitmapContextCreate(NULL, targetSize.width, targetSize.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);    

    CGContextDrawImage(bitmap, CGRectMake(1, 1, targetSize.width-2, targetSize.height-2), imageRef);
    CGImageRef ref = CGBitmapContextCreateImage(bitmap);
    UIImage* newImage = [UIImage imageWithCGImage:ref];

    CGContextRelease(bitmap);
    CGImageRelease(ref);

    NSData * thumbData = UIImagePNGRepresentation(newImage);
    NSData * imageData = UIImagePNGRepresentation(orig);

    //Post notification on main thread since it will be updating the UI
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        self.tempImageThumbStorage = newImage;
        self.imageThumbnailData = thumbData;
        self.tempImageStorage = orig;
        self.imageData = imageData;

        [(AppDelegate*)[UIApplication sharedApplication].delegate saveContext];

        NSMutableDictionary * userInfo = [[NSMutableDictionary alloc] initWithCapacity:1];
        [userInfo setObject:[NSNumber numberWithBool:YES] forKey"status"];
        [userInfo setObject:self forKey"comic"];

        [[NSNotificationCenter defaultCenter] postNotificationNameCRLoadMediahNotification object:self userInfo:userInfo];
        strongSelf = nil;
    }];

}



Best Answer-推荐答案


原来我过于频繁地保存上下文,这导致 imageData 无法正确保存到磁盘,并导致异常。

关于iphone - 什么可能导致 [UIImage imageWithData] 返回时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8771928/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4