iosuiimagecgimageref

UIImage Not Splitting Properly


I am trying to split a UIImage in half horizontally, keeping the original width of the image, but the returned image is the top left corner of the original.

CGImageRef imageRef = image.CGImage;

CGImageRef topR = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0,  image.size.width, image.size.height/2.0));
UIImage *topImage = [UIImage imageWithCGImage:topR];
CGImageRelease(topR);

When I set the rect to (0,0, 1000, image.size.height/2) it returns the full width of the image. I've printed image.size.width and it returns the correct width but createImageInRect is returning the top corner.


Solution

  • I used CGImageGetWidth instead and now it works

     CGImageRef topR = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, CGImageGetWidth(imageRef), CGImageGetHeight(imageRef)/2));