UIImageView IOS中间的透明圆圈

时间:2013-11-19 05:46:58

标签: ios iphone objective-c uiimageview uiimage

我需要让UIImageView在中间位置透明(圆形圆圈..参见附图)enter image description here
我应该可以通过那个中间圈看到UIImageView的背后子视图......我试过了CAShapeLayer ..但是我无法看到UIImageView的背后子视图。请帮助我......请为我的英语......

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

2 个答案:

答案 0 :(得分:3)

这样做的一种方法就是这样。我的主图像有一个图像视图,另一个叫做BlackCircle的图像只是一个40x40的黑色圆圈(我在Pixen应用程序中制作)。动作方法使主图像在黑色圆圈重叠的地方变得透明,黑色圆圈位于主图像的中心位置:

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

答案 1 :(得分:1)

不要试图让Imageview的一部分透明,而是尝试通过将相应的alpha设置为0来使图像在您想要的区域透明。