1

我使用此代码制作了圆形蒙版效果:

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

当我设置时size--,圆圈缩小了值,但是当我设置size++圆圈时没有放大值。这是怎么回事? 在此处输入图像描述

4

1 回答 1

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.

于 2013-03-25T09:13:50.377 回答