CGImage到UIImage不工作

编程入门 行业动态 更新时间:2024-10-28 17:29:02
本文介绍了CGImage到UIImage不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发和iOS应用程序为iPad和我使用Grabkit,以便从Facebook,Twitter,Flicker和相机胶卷的图像。要获取最后一个图像,我需要将一个CGImage转换为UIImage,但我遇到了麻烦。就像如果我没有得到任何图像,因为当我后来用的UIImage,应用程序崩溃与此日志:

I'm developing and iOS app for iPad and I use Grabkit in order to get images from Facebook, Twitter, Flicker and also the Camera Roll. To get the images from the last one, I need to convert a CGImage to an UIImage, but I'm having trouble with that. Is like if I didn't get any image, because when I use the UIImage later, the app crashes with this log:

*** Terminating app due to uncaught exception 'CALayerInvalidGeometry', reason: 'CALayer position contains NaN: [nan 653]'

我用下面的代码的转换CGImage:

I'm using the following code to convert the CGImage:

UIImage* image = [UIImage imageWithCGImage:imgRef];

所以这段代码不会崩溃,但是当我使用创建的映像时,发生什么事?是否错误?

So this code doesn't crash, but when I use the image created, it does. What is happening? Is the code wrong?

推荐答案

您应该使用alloc init like UIImage * myImage = [[UIImage alloc ] initWithCGImage:myCGImage];

You should use alloc init like UIImage* myImage = [[UIImage alloc] initWithCGImage:myCGImage];

或者您可以尝试:

CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); NSUInteger width = CGImageGetWidth(image); NSUInteger height = CGImageGetHeight(image); NSUInteger bytesPerPixel = 4; NSUInteger bytesPerRow = bytesPerPixel * width; NSUInteger bitsPerComponent = 8; int size = height*width*bytesPerPixel; unsigned char *rawData = malloc(size); CGContextRef context = CGBitmapContextCreate(rawData,width,height,bitsPerComponent,bytesPerRow,colorSpace,kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big); CGColorSpaceRelease(colorSpace); CGContextDrawImage(context, CGRectMake(0,0,width,height),image); UIImage *newImage = [UIImage imageWithCGImage:CGBitmapContextCreateImage(context)]; CGContextRelease(context); free(rawData);

更多推荐

CGImage到UIImage不工作

本文发布于:2023-07-30 16:25:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1251024.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:工作   CGImage   UIImage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!