imagewithcgimage始终在objective-c中返回错误的访问内存错误

imagewithcgimage始终在objective-c中返回错误的访问内存错误,objective-c,xcode,uikit,Objective C,Xcode,Uikit,我正在做的是从位图数据创建CGImage并将其转换为UIImage 我有下面的代码 - (UIImage *)imageWithBitmapBytes:(Byte *)imageBytes width:(size_t)width height:(size_t)height pixelCount:(NSUInteger)pixelCount { NSUInteger imageBufferSize = width * height * pixelCount; CGDataProv

我正在做的是从位图数据创建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];
我认为问题在于:

NSUInteger imageBufferSize = width * height * pixelCount;
因为,imageBufferSize应该是imageBytes的长度

希望对你有帮助