gpt4 book ai didi

objective-c - UIImage 的 CGImage 返回 NULL

转载 作者:可可西里 更新时间:2023-11-01 03:27:29 26 4
gpt4 key购买 nike

我创建了一个将图像拆分为多个图像的函数,但是当我获取 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;



}

最佳答案

我已经从 CGImage 创建了 UIImage

CIImage *ciImage = image.CIImage;
CIContext *context = [CIContext contextWithOptions:nil];
CGImageRef ref = [context createCGImage:ciImage fromRect:ciImage.extent];
UIImage *newImage = [UIImage imageWithCGImage:ref];

现在 newImage.CGImage 不是 nil

关于objective-c - UIImage 的 CGImage 返回 NULL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10022534/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com