[英]CGImage of UIImage return 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我创建了一个 function 用于将图像拆分为多个图像,但是当我拍摄 UIImage 的 CGImage 时,CGImage 返回 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;
}
I have created UIImage
from CGImage
.我从CGImage
UIImage
CIImage *ciImage = image.CIImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef ref = [context createCGImage:ciImage fromRect:ciImage.extent];
UIImage *newImage = [UIImage imageWithCGImage:ref];
And now newImage.CGImage
is not nil
现在newImage.CGImage
不是nil
The CGImage property will return nil if the UIImage was created from another image such as an IOSurface or CIImage.如果 UIImage 是从另一个图像(例如 IOSurface 或 CIImage)创建的,则 CGImage 属性将返回 nil。 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.为了在这种特殊情况下解决这个问题,我可以使用 c function 从 IOSurface 创建一个 CGImage,然后将其转换为 UIImage。
UICreateCGImageFromIOSurface(IOSurfaceRef surface);
Convert CGImage to UIImage with this and cgImage will not be null:使用此将 CGImage 转换为 UIImage,cgImage 将不是 null:
func convert(cmage:CIImage) -> UIImage
{
let context:CIContext = CIContext.init(options: nil)
let cgImage:CGImage = context.createCGImage(cmage, from: cmage.extent)!
let image:UIImage = UIImage.init(cgImage: cgImage)
return image
}
What you did now is just create pieces with 0.f width , you should use two for
s to define the width
& height
for your pieces.您现在所做的只是创建宽度为 0.f的片段,您应该使用两个for
来定义片段的width
和height
。 Code sample like this (not tested yet, but it should works):像这样的代码示例(尚未测试,但它应该可以工作):
for (int height = 0; height < image.size.height; height += piecesSize) {
for (int width = 0; width < image.size.width; width += piecesSize) {
CGRect subFrame = CGRectMake(width, height, piecesSize, piecesSize);
CGImageRef newImage = CGImageCreateWithImageInRect(image.CGImage, subFrame);
UIImage * finalImage = [UIImage imageWithCGImage:newImage];
CGImageRelease(newImage);
[tempArray addObject:finalImage];
}
}
It happens in some cases when we try to crop image.在某些情况下,当我们尝试裁剪图像时会发生这种情况。 I found the solution like this try this may be this can help you:-我找到了这样的解决方案,试试这可能对您有帮助:-
NSData *imageData = UIImageJPEGRepresentation(yourImage, 0.9);
newImage = [UIImage imageWithData:imageData];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.