UIImage的CGImage返回NULL

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

我创建了一个将图像分割成多个图像的函数,但是当我拍摄UIImage的CGImage时,CGImage返回NULL

I created a function for splitting an image into multiple images, but when I take take the CGImage of the UIImage, the CGImage returns NULL

NSArray* splitImage(UIImage* image,NSUInteger pieces) { NSLog(@"width: %f, %zu",image.size.width,CGImageGetWidth(image.CGImage)); NSLog(@"%@",image.CGImage); returns NULL NSMutableArray* tempArray = [[NSMutableArray alloc]initWithCapacity:pieces]; CGFloat piecesSize = image.size.height/pieces; for (NSUInteger i = 0; i < pieces; i++) { // take in account retina displays CGRect subFrame = CGRectMake(0,i * piecesSize * image.scale ,image.size.width * image.scale,piecesSize * image.scale); CGImageRef newImage = CGImageCreateWithImageInRect(image.CGImage,subFrame); UIImage* finalImage =[UIImage imageWithCGImage:newImage]; CGImageRelease(newImage); [tempArray addObject:finalImage]; } NSArray* finalArray = [NSArray arrayWithArray:tempArray]; [tempArray release]; return finalArray; }

推荐答案

如果UIImage是从另一个图像(如IOSurface或CIImage)创建的,则CGImage属性将返回nil。为了解决这个问题,我可以使用c函数从IOSurface创建一个CGImage,然后将其转换为UIImage。

The CGImage property will return nil if the UIImage was created from another image such as an IOSurface or CIImage. To get around this in this particular case I can create a CGImage from an IOSurface using the c function then convert that to a UIImage.

UICreateCGImageFromIOSurface(IOSurfaceRef surface);

更多推荐

UIImage的CGImage返回NULL

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

发布评论

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

>www.elefans.com

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