我正在做的是从位图数据创建CGImage并将其转换为UIImage。
我下面有代码
- (UIImage *)imageWithBitmapBytes:(Byte *)imageBytes width:(size_t)width height:(size_t)height pixelCount:(NSUInteger)pixelCount {
NSUInteger imageBufferSize = width * height * pixelCount;
CGDataProviderRef dataProviderRef = CGDataProviderCreateWithData(nil, imageBytes, imageBufferSize, nil);
NSUInteger bitsPerComponent = 8;
NSUInteger bitsPerPixel = bitsPerComponent * pixelCount;
NSUInteger bytesPerRow = width * pixelCount;
CGImageRef cgImage = CGImageRetain(CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, CGColorSpaceCreateDeviceGray(), kCGImageAlphaLast, dataProviderRef, nil, false, kCGRenderingIntentDefault));
CGDataProviderRelease(dataProviderRef);
UIImage *image =[UIImage imageWithCGImage:cgImage];
CGImageRelease(cgImage);
return image;
}
我在打开和关闭ARC的两种情况下都测试了此代码。
我总是在此行收到错误的访问内存错误
UIImage *image =[UIImage imageWithCGImage:cgImage];
答案 0 :(得分:1)
我认为问题是:
NSUInteger imageBufferSize = width * height * pixelCount;
因为 imageBufferSize 的长度应为 imageBytes ...
希望对您有帮助