gpt4 book ai didi

iphone - 返回时可能导致 [UIImage imageWithData] 崩溃的原因

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:24:03 24 4
gpt4 key购买 nike

我的一个 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在控制台中,所以我假设 NSMananagedObject 本身已被释放。没有意义的是它能够为 2 个 ifs 引用 self,我什至在它之前记录图像数据及其长度。

NSManagedObject 怎么可能在那段时间被释放,因为这一切都发生在同一个线程(而且它是主线程)上?

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

设置图片数据的代码:

- (void) processRequest:(ASIHTTPRequest *) 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] postNotificationName:PCRLoadMediahNotification object:self userInfo:userInfo];
strongSelf = nil;
}];

}

最佳答案

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

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com