gpt4 book ai didi

ios - QCAR::Image 到 UIImage - CoreGraphics 并保留崩溃

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:17:43 25 4
gpt4 key购买 nike

我正在使用 Vuforia,尝试从相机实例获取像素并转换为 UIImage

- (UIImage *)createUIImage:(const QCAR::Image *)qcarImage
{
int width = qcarImage->getWidth();
int height = qcarImage->getHeight();
int bitsPerComponent = 8;
int bitsPerPixel = QCAR::getBitsPerPixel(QCAR::RGB888);
int bytesPerRow = qcarImage->getBufferWidth() * bitsPerPixel / bitsPerComponent;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault | kCGImageAlphaNone;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;

CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, qcarImage->getPixels(), QCAR::getBufferSize(width, height, QCAR::RGB888), NULL);

CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGImageRef imageRefRetain = CGImageRetain(imageRef);
UIImage *image = [UIImage imageWithCGImage:imageRefRetain];

CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);

return image;
}

获取图像后,我将其添加到一个可变数组中。

从可变数组中检索图像后,我在每种方法上都会崩溃即

UIImageView setImage: 

甚至打电话

NSData* comppressedData = UIImageJPEGRepresentation(image,1);

让我的应用程序不崩溃的唯一方法是添加以下内容

- (UIImage *)createUIImage:(const QCAR::Image *)qcarImage
{
...
UIImage *image = [UIImage imageWithCGImage:imageRefRetain];

CGDataProviderRelease(provider);
CGColorSpaceRelease(colorSpaceRef);
CGImageRelease(imageRef);

NSData* comppressedData = UIImageJPEGRepresentation(image,1);
UIImage* jpegImage = [UIImage imageWithData:comppressedData];
return jpegImage;
}

所以我认为这是与 CGImageRef 发布相关的问题。我真的需要了解发生了什么,以及为什么在存在该方法后图像引用无效。

最佳答案

问题是 qcarImage 超出范围并带走了原始图像数据。复制图像缓冲区并将其提供给 CGDataProviderRef

NSData * imageData = [NSData dataWithBytes:qcarImage->getPixels() 
length:QCAR::getBufferSize(width, height, QCAR::RGB888)];
CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, imageData.bytes, imageData.length, NULL);
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
//This was leaking memory, commenting out
//CGImageRef imageRefRetain = CGImageRetain(imageRef);
UIImage *image = [UIImage imageWithCGImage:imageRef];

关于ios - QCAR::Image 到 UIImage - CoreGraphics 并保留崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25426072/

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