【问题标题】:CGImage of UIImage return NULLUIImage 的 CGImage 返回 NULL
【发布时间】:2023-03-09 17:13:01
【问题描述】:

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



}

【问题讨论】:

标签: objective-c ios uiimage cgimage


【解决方案1】:

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

UICreateCGImageFromIOSurface(IOSurfaceRef surface);

【讨论】:

    【解决方案2】:

    我从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

    【讨论】:

      【解决方案3】:

      您现在所做的只是创建具有 0.f 宽度的片段,您应该使用两个fors 来为您的片段定义widthheight。像这样的代码示例(尚未测试,但应该可以):

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

      【讨论】:

      • +1 我有(经过测试的)代码可以做到这一点,它符合您的建议。我的被​​打包为 UIImage 上的一个类别方法,称为 subimageInRect:(CGRect)rect。我认为这是一种更漂亮的方式。
      【解决方案4】:

      在某些情况下,当我们尝试裁剪图像时会发生这种情况。我找到了这样的解决方案试试这可能对你有帮助:-

      NSData *imageData = UIImageJPEGRepresentation(yourImage, 0.9);
      newImage = [UIImage imageWithData:imageData];
      

      【讨论】:

        【解决方案5】:

        用这个将 CGImage 转换为 UIImage 并且 cgImage 不会为空:

        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
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2012-11-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2014-11-08
          • 1970-01-01
          相关资源
          最近更新 更多