ios - UIImageView IOS中间的透明圆圈

标签 ios iphone objective-c uiimageview uiimage

我需要使 UIImageView 在中间位置透明(圆圈 ..见附图)enter image description here
我应该能够通过那个中间的圆圈看到 UIImageView 的背后 subview ...我试过 CAShapeLayer..但是我看不到 UIImageView 的背后 subview ..请帮助我..对不起我的英语....

    CAShapeLayer *circleLayer = [CAShapeLayer layer];

    // Give the layer the same bounds as your image view
    [circleLayer setBounds:CGRectMake(100.0f, 100.0f, [((UIImageView *)view) bounds].size.width,
                                      [((UIImageView *)view) bounds].size.height)];
    // Position the circle anywhere you like, but this will center it
    // In the parent layer, which will be your image view's root layer
    [circleLayer setPosition:CGPointMake(100,
                                         100)];
    // Create a circle path.
    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:
                          CGRectMake(100.0f, 100.0f, 50.0f, 50.0f)];
    // Set the path on the layer
    [circleLayer setPath:[path CGPath]];
    // Set the stroke color
    [circleLayer setStrokeColor:[[UIColor redColor] CGColor]];
    // Set the stroke line width
    [circleLayer setLineWidth:2.0f];

    // Add the sublayer to the image view's layer tree
    [circleLayer setOpacity:0.5];
[[((UIImageView *)view) layer] addSublayer:circleLayer];

最佳答案

一种方法是这样的。我有一个 ImageView ,其中包含我的主图像和另一个名为 BlackCircle 的图像,它只是一个 40x40 的黑色圆圈(我在 Pixen 应用程序中制作的)。 Action 方法使主图像在与黑色圆圈重叠的位置变得透明,我将其放置在主图像的中心:

@interface ViewController ()
@property (weak,nonatomic) IBOutlet UIImageView *imageView;
@end

@implementation ViewController

-(void)viewDidLoad {
    [super viewDidLoad];
    self.imageView.image = [UIImage imageNamed:@"House.tiff"];
    self.imageView.backgroundColor = [UIColor clearColor];
}


-(IBAction)createAlphaClippedImage:(id)sender {
    UIImage *circleImage = [UIImage imageNamed:@"BlackCircle.png"];
    UIImage *mainImage = [UIImage imageNamed:@"House.tiff"];

    UIGraphicsBeginImageContextWithOptions(mainImage.size, NO, 0.0);

    CGRect imageRect = CGRectMake(0.0f, 0.0f, mainImage.size.width, mainImage.size.height);
    CGRect centerCircleRect = CGRectMake(mainImage.size.width/2.0 - circleImage.size.width/2.0, mainImage.size.height/2.0 - circleImage.size.height/2.0,circleImage.size.width,circleImage.size.height);

    [mainImage drawInRect:imageRect];
    [circleImage drawInRect:centerCircleRect blendMode:kCGBlendModeXOR alpha:1.0];

    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.imageView.image = image;
}

关于ios - UIImageView IOS中间的透明圆圈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20064115/

相关文章:

objective-c - ios多人策略: Sending packets with ARC,(无结构)

ios - Nativescript - 引用错误 : FIRAuth

Ios 模拟器在尝试加载 Storyboard 时给我一个黑屏?

ios - 通过 Apple 的 ToDoList 应用程序教程,点击项目不会正确添加 "completed"复选标记。

iphone - 如何在 iOS 应用程序中显示和编辑现有的 PDF 文件

javascript - pc手机上网浏览器

objective-c - 后台网络调用 - iOS

iphone - UIScrollView 中的 UIScrollView

objective-c - 用于编写 Objective-C 代码的良好 xcode 替代方案

ios - 当使用 ARC 从 UIView 调用 dealloc 时,我可以假设 ivars 仍然保留吗?