本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 10:53