我正在开发和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不工作
发布评论