Split UIImage in half?
Asked Answered
A

3

12

How would I go about splitting a UIImage In half(down the middle) so it would make two images?

Anacreontic answered 19/5, 2012 at 0:19 Comment(0)
D
22

You can try this,

UIImage *image = [UIImage imageNamed:@"yourImage.png"];
CGImageRef tmpImgRef = image.CGImage;
CGImageRef topImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, 0, image.size.width, image.size.height / 2.0));
UIImage *topImage = [UIImage imageWithCGImage:topImgRef];
CGImageRelease(topImgRef);

CGImageRef bottomImgRef = CGImageCreateWithImageInRect(tmpImgRef, CGRectMake(0, image.size.height / 2.0,  image.size.width, image.size.height / 2.0));
UIImage *bottomImage = [UIImage imageWithCGImage:bottomImgRef];
CGImageRelease(bottomImgRef);

hope this can help you, :)

Dora answered 19/5, 2012 at 4:11 Comment(0)
A
7
- (void)splitImage:(UIImage *)image
{
    CGFloat imgWidth = image.size.width/2;
    CGFloat imgheight = image.size.height;

    CGRect leftImgFrame = CGRectMake(0, 0, imgWidth, imgheight);
    CGRect rightImgFrame = CGRectMake(imgWidth, 0, imgWidth, imgheight);

    CGImageRef left = CGImageCreateWithImageInRect(image.CGImage, leftImgFrame);
    CGImageRef right = CGImageCreateWithImageInRect(image.CGImage, rightImgFrame);

    // These are the images we want!
    UIImage *leftImage = [UIImage imageWithCGImage:left];
    UIImage *rightImage = [UIImage imageWithCGImage:right];

    // Don't forget to free the memory!
    CGImageRelease(left);
    CGImageRelease(right);
}
Athanasia answered 14/2, 2015 at 14:53 Comment(1)
This is the only answer that does not create a whole bunch of contexts. Quartz does a great job (getting the image's pixel data straight away, instead of making a new context, draw image, then crop) at optimising the cropping process. This is potentially many times faster when we are splitting images into many small parts!Ostend
E
4

iOS 11, swift 4.0 updated version https://stackoverflow.com/users/893872/durul-dalkanat which I think to be honest is the best :)

This splits an image into 4 parts

func splitImage(image2D: UIImage) -> [UIImage] {
    let imgWidth = image2D.size.width / 2
    let imgHeight = image2D.size.height / 2
    var imgImages:[UIImage] = []

    let leftHigh = CGRect(x: 0, y: 0, width: imgWidth, height: imgHeight)
    let rightHigh = CGRect(x: imgWidth, y: 0, width: imgHeight, height: imgHeight)
    let leftLow = CGRect(x: 0, y: imgHeight, width: imgWidth, height: imgHeight)
    let rightLow = CGRect(x: imgWidth, y: imgHeight, width: imgWidth, height: imgHeight)

    let leftQH = image2D.cgImage?.cropping(to:leftHigh)
    let rightHQ = image2D.cgImage?.cropping(to:rightHigh)
    let leftQL = image2D.cgImage?.cropping(to:leftLow)
    let rightQL = image2D.cgImage?.cropping(to:rightLow)

    imgImages.append(UIImage(cgImage: leftQH!))
    imgImages.append(UIImage(cgImage: rightHQ!))
    imgImages.append(UIImage(cgImage: leftQL!))
    imgImages.append(UIImage(cgImage: rightQL!))

    return imgImages
}
Elviaelvie answered 18/11, 2017 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.