当前位置:编程学习 > wap >>

iphone中的圆形相框效果

想用下面的代码实现一个圆形相框:

[plain]
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(); 

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++的时候,就没有反应。
不知道怎么呢?

 \

解决方案:

你的代码修改成这样就可以了

[plain]
- (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(); 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,