如何以编程方式提取图像的子图像

时间:2013-06-15 14:22:49

标签: objective-c uiimage cgimageref

我尝试使用Objective-C获取UIImage的一部分,但无法找到有关如何执行此操作的任何内容。我想帮助弄清楚如何做到这一点以及所涉及的步骤。

1 个答案:

答案 0 :(得分:2)

试试这个..这将图像分成两部分,更具体地说是一半。

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

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