CGImage of UIImage return NULL

纵饮孤独 提交于 2019-12-01 02:05:12

问题


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;



}

回答1:


I have created UIImage from CGImage.

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




回答2:


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);



回答3:


What you did now is just create pieces with 0.f width, you should use two fors to define the width & height for your pieces. 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];
  }
}



回答4:


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];



回答5:


Convert CGImage to UIImage with this and cgImage will not be 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
}


来源:https://stackoverflow.com/questions/10022534/cgimage-of-uiimage-return-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!