UIImageView IOS中间的透明圆圈 [英] Transparent Circle at Middle of UIImageView IOS

查看:75
本文介绍了UIImageView IOS中间的透明圆圈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在中间位置使UIImageView透明(圆形...请参阅所附图像)
我应该能够通过该中间圈看到UIImageView的子视图...我尝试了CAShapeLayer ..但是我看不到UIImageView的子视图..请帮助我..抱歉我的英语....

I need to make UIImageView transparent at the Middle Position(Round Circle ..See attached image)
I should be able to see the Behind Subviews of UIImageView via that middle circle...i tried CAShapeLayer..but i am not able to see the behind subviews of UIImageView..Please help me..Sorry for my English....

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

推荐答案

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

One way to do it is like this. I have an image view with my main image, and another image called BlackCircle which is just a 40x40 black circle (that I made in the Pixen app). The action method causes the main image to become transparent where it is overlapped by the black circle, which I've placed in the center of the main image:

@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;
}

这篇关于UIImageView IOS中间的透明圆圈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆