1

I use this code to made a circle mask effect:

CGContextRef context = CGBitmapContextCreate(NULL, self.bounds.size.width, self.bounds.size.height, 8, 4 * self.bounds.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst);
CGContextAddArc(context, 1024/2, 768/2, size, 0, 6.3, 0);
CGContextClosePath(context);
CGContextClip(context);
CGContextDrawImage(context, self.bounds, imageView.image.CGImage);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
CGImageRelease(imageMasked);

[imageView setImage:newImage];
UIGraphicsEndImageContext();

when i set size--,the circle zoom out with the value,but when i set size++the circle no zoom in with the value.What is going on? enter image description here

Haven Lin
  • 205
  • 1
  • 9
  • This doesn't answer your question but may be worth a browse: [Adding a circle mask layer on an UIImageView](http://stackoverflow.com/questions/14670985/adding-a-circle-mask-layer-on-an-uiimageview/15429140#15429140). A circular mask is added to an image that can be expanded and contracted through user interaction. It is very performant. – Elliott Mar 25 '13 at 08:41
  • If you want zoom effect in masking then u have to always mask it, else just zoom in / out image containing image – DivineDesert Mar 25 '13 at 09:06
  • @ElliottPerry nice observation! thanks for your help :) – Haven Lin Mar 25 '13 at 09:46

1 Answers1

2

I think not setting the original image first is your problem. I tested your code a little, and made it work two ways:

- (IBAction)minus:(id)sender {
    self.radius = @([self.radius floatValue]+10.0f);
    [self redrawImage];
}

- (IBAction)plus:(id)sender {
    self.radius = @([self.radius floatValue]-10.0f);
    [self redrawImage];
}

-(void)redrawImage
{
    //set the original image first
    [_imageView setImage:[UIImage imageNamed:@"your image goes here"]];

    CGFloat size = [self.radius floatValue];
    CGContextRef context = CGBitmapContextCreate(NULL, self.view.bounds.size.width, self.view.bounds.size.height, 8, 4 * self.view.bounds.size.width, CGColorSpaceCreateDeviceRGB(), kCGImageAlphaPremultipliedFirst);
    CGContextAddArc(context, 320/2, 460/2, size, 0, 2*M_PI, 0);
    CGContextClosePath(context);
    CGContextClip(context);
    CGContextDrawImage(context, self.view.bounds, _imageView.image.CGImage);
    CGImageRef imageMasked = CGBitmapContextCreateImage(context);
    CGContextRelease(context);
    UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
    CGImageRelease(imageMasked);

    [_imageView setImage:newImage];
    UIGraphicsEndImageContext();
}

Also instead of setting 6.3, it's easier to use 2 * M_PI for the angle.

Michał Zygar
  • 4,052
  • 1
  • 23
  • 36