CGImage of UIImage return NULL

后端 未结 5 681
情深已故
情深已故 2021-01-12 05:16

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* splitIma         


        
5条回答
  •  时光说笑
    2021-01-12 05:59

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

提交回复
热议问题