我有一個動態的字節的緩衝區創建的UIImage和分配新創建的UIImage到UIImageView.image屬性的代碼時:獲得EXC_BAD_ACCESS試圖用dynamicaly創建的UIImage中的UIImageView
UIImage* UIImageFromDibRect(long w, long h) {
size_t dataSiz = w * h * 4;
std::unique_ptr<uint8> data(new uint8[dataSiz]);
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, data.get(), dataSiz, NULL);
// set up for CGImage creation
size_t bitsPerComponent = 8;
size_t bitsPerPixel = 32;
size_t bytesPerRow = 4 * w;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(w, h, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
// make UIImage from CGImage
UIImage *uiImage = [UIImage imageWithCGImage:imageRef];
return uiImage;
}
[...]
@interface ScreenViewController()
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@end
[...]
@implementation ScreenViewController
-(void)myMethod {
UIImage* newImage = UIImageFromDibRect(128, 128);
self.imageView.image = newImage;
}
@end
所以,所有的功能都恰到好處仿真器和在我的iPhone 6S,但是崩潰在我的iPad 2瀏覽:
0x26bd3182 <+138>: ldr r0, [r2]
0x26bd3184 <+140>: blx 0x27507218 ; symbol stub for: -[_UIRemoteViewController(_UIRemoteViewController_AutomaticInvaldiation) autorelease]
> 0x26bd3188 <+144>: mov r0, r5
0x26bd318a <+146>: blx 0x27507268 ; symbol stub for: __strlcpy_chk$shim
0x26bd318e <+150>: mov r0, r4
調用堆棧是空的,即開始 - > main-> UIApplicationMain
我爲iPhone 6s和iPad2使用iOS 9.3.3。目標平臺是iOS 9.0
btw。創建後UIImage看起來不錯。如果我喜歡的東西取代我UIImageFromDibRect:
UIImage* UIImageFromDibRect(long w, long h) {
CGRect r = CGRectMake(0.0f, 0.0f, w, h);
UIGraphicsBeginImageContext(r.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [[UIColor redColor] CGColor]);
CGContextFillRect(context, r);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
然後一切工作就好了。所以,我懷疑我在做什麼毛病CGImageCreate(...)或CGDataProviderCreateWithData(...)
任何想法?
謝謝!
不,它不能解決我的問題。正如我所說的,第二種方法可以很好地工作,但它不適用於第一種方法,而且僅適用於iPad 2.它看起來不像是資源泄漏問題,因爲它一開始就崩潰了。但感謝提示。 – Tutankhamen
@Tutankhamen:你試過了嗎?CFRelease(imageRef);'? – Lion
是的,我做過。我已經添加了CFRelease(imageRef)和CGColorSpaceRelease(colorSpaceRef),但我仍然得到相同的異常... – Tutankhamen