iphone – 擦除UIImageView的矩形

我想知道,我如何擦除UIImageView的自定义rect(例如,在IB或其他东西中使用UIView)以显示位于下方的其他UIImageView.

我没有设法在论坛中使用一些回复…

最佳答案
这将清除图像中的矩形:

- (UIImage *)clearRect:(CGRect)rect inImage:(UIImage *)image {

    if (UIGraphicsBeginImageContextWithOptions != NULL)
        UIGraphicsBeginImageContextWithOptions([image size], NO, 0.0);
    else
        UIGraphicsBeginImageContext([image size]);

    CGContextRef context = UIGraphicsGetCurrentContext();

    [image drawInRect:CGRectMake(0.0, 0.0, [image size].width, [image size].height)];
    CGContextClearRect(context, rect);

    UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return result;
}

只需加载图像并清除图像,然后再将其指定给图像视图:

UIImage *image = [UIImage imageNamed:@"Image.png"];
UIImage *maskedImage = [self clearRect:CGRectMake(10.0, 10.0, 50.0, 50.0) inImage:image];
[imageView setImage:maskedImage];

转载注明原文:iphone – 擦除UIImageView的矩形 - 代码日志