9

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;



}
Otium
  • 1,098
  • 8
  • 20
  • check tempArray is null or not in last – Hector Apr 05 '12 at 04:22
  • 1
    CGImage is not an object, it is a C struct, so you should not be using a NSLog with %@. – lnafziger Apr 05 '12 at 04:24
  • The problem was that I created the UIImage with an IOSurface :D – Otium Apr 05 '12 at 04:28
  • If you want to ask a different question, please create a new, not edited here.. :) – Kjuly Apr 05 '12 at 04:28
  • @lnafziger: The structure is an implementation detail, with its members not visible outside of Core Graphics. CGImages are Core Foundation objects, and the documentation explicitly says that `%@` works with both Objective-C objects and Core Foundation objects. http://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/Strings/Articles/formatSpecifiers.html http://developer.apple.com/library/ios/documentation/CoreFoundation/Conceptual/CFStrings/formatSpecifiers.html – Peter Hosey Dec 18 '12 at 02:57
  • @PeterHosey: I didn't realize that %@ worked with CF objects. Thanks! – lnafziger Dec 19 '12 at 05:42

5 Answers5

9

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

Andrew Bogaevskyi
  • 2,251
  • 21
  • 25
7

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);
Otium
  • 1,098
  • 8
  • 20
4

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
}
Ricardo
  • 2,086
  • 25
  • 35
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];
  }
}
Kjuly
  • 34,476
  • 22
  • 104
  • 118
  • 1
    +1 I have (tested) code that does this, and it matches your suggestion. Mine is packaged as a category method on UIImage called subimageInRect:(CGRect)rect. I think that's a prettier way to go. – danh Apr 05 '12 at 04:28
1

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];
Leena
  • 2,678
  • 1
  • 30
  • 43