【问题标题】:Erase a rect of an UIImageView擦除 UIImageView 的矩形
【发布时间】:2011-09-12 19:15:04
【问题描述】:

我想知道,如何删除 UIImageView 的自定义矩形(例如,IB 中的 UIView 或其他内容)以显示位于下方的另一个 UIImageView


我没有设法通过论坛中的一些回复来做到这一点......

【问题讨论】:

标签: iphone objective-c ios ipad uiimageview


【解决方案1】:

这将清除图像中的矩形:

- (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];

【讨论】:

    【解决方案2】:

    您无法清除 UIImageView 本身,因为这只是绘制 UIImage。所以你已经擦除了 UIImage 中的矩形。创建位图上下文,将图像绘制到其中。用 CGContextClearRect 擦除你想要“透视”的部分。从位图上下文创建新图像时。

    【讨论】:

      【解决方案3】:

      可能不是最好的解决方案,但您可以采取另一种方式,分别取矩形周围的 4 个部分,然后在没有内部矩形的情况下将它们组合起来。只要您要裁剪出矩形,就可以重复此操作。

      【讨论】:

      • 其实我找到了QuarzCore或者CoreGraphic的方式。但是是的,它可以完成这项工作。
      猜你喜欢
      • 1970-01-01
      • 2011-11-24
      • 1970-01-01
      • 2016-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多