将UIImage分成两半?

Shr*_*794 9 xcode cocoa-touch ipad ios xcode4.3

我怎样才能将UIImage分成两半(从中间开始),这样就可以生成两张图像?

xda*_*001 22

你可以试试这个,

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);
Run Code Online (Sandbox Code Playgroud)

希望这可以帮到你, :)


Dur*_*nat 6

- (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);
}
Run Code Online (Sandbox Code Playgroud)