2016-07-26 65 views
0

我有一个动态的字节的缓冲区创建的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(...)

任何想法?

谢谢!

回答

0

所以首先,创建后CGColorSpaceRefCGImageRef保持数增加,ARC将不会被自己释放,它可能会导致泄漏,你需要一个电话CGColorSpaceRelease(colorSpaceRef)CFRelease(imageRef);CGDataProviderRelease(provider);

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]; 

    CFRelease(imageRef); 
    CGColorSpaceRelease(colorSpaceRef); 
    CGDataProviderRelease(provider); 
    return uiImage; 
} 

和在你的第二个方法CGContextRef需要释放相同CGContextRelease(context);

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(); 

    CGContextRelease(context); 

    return image; 
} 

希望这有助于解决您的崩溃

+0

不,它不能解决我的问题。正如我所说的,第二种方法可以很好地工作,但它不适用于第一种方法,而且仅适用于iPad 2.它看起来不像是资源泄漏问题,因为它一开始就崩溃了。但感谢提示。 – Tutankhamen

+0

@Tutankhamen:你试过了吗?CFRelease(imageRef);'? – Lion

+0

是的,我做过。我已经添加了CFRelease(imageRef)和CGColorSpaceRelease(colorSpaceRef),但我仍然得到相同的异常... – Tutankhamen