将UiImageView水平分割成两半?

时间:2014-02-18 01:49:47

标签: ios uiimageview split uiimage declare

我如何将UIImageView水平分成两半?我不想要两个单独的图像,但更多的只是声明UiImageView的两半。我的最终目的是,如果我的“球”击中平台的顶部(上半部分),它会反弹。如果球击中平台的下半部分,它会反弹。

1 个答案:

答案 0 :(得分:0)

- (void)split
{
    UIImage *image = [UIImage imageNamed:@"cat.jpeg"];
    CGImageRef imageRef = image.CGImage;

    CGImageRef leftRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(0, 0, image.size.width/2, image.size.height));
    UIImage *leftImage = [UIImage imageWithCGImage:leftRef];
    CGImageRelease(leftRef);

    _imageView1.image = leftImage;

    CGImageRef rightRef = CGImageCreateWithImageInRect(imageRef, CGRectMake(image.size.width /2, 0,  image.size.width , image.size.height));
    UIImage *rightImage = [UIImage imageWithCGImage:rightRef];
    CGImageRelease(rightRef);

    _imageView2.image = rightImage;
}