iphone中的圆形相框效果

问答小助手 2013-04-02 11:27:44
加精
原问题来自于CSDN问答频道,更多解决方案见:http://ask.csdn.net/questions/1908

想用下面的代码实现一个圆形相框:
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++的时候,就没有反应。
不知道怎么呢?




解决方案:

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

- (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();
}
...全文
3629 64 打赏 收藏 转发到动态 举报
写回复
用AI写文章
64 条回复
切换为时间正序
请发表友善的回复…
发表回复
凛然。 2015-09-13
  • 打赏
  • 举报
回复
每次都需要重新绘制就好了
zhuobattle 2014-02-17
  • 打赏
  • 举报
回复
xia61972612 2014-02-16
  • 打赏
  • 举报
回复
好屌!我完全看不懂
NULL 2014-02-15
  • 打赏
  • 举报
回复
[image.layer setCornerRadius:17]; [image.layer setMasksToBounds:TRUE]; 这两句就可以了
junfeng531 2014-02-14
  • 打赏
  • 举报
回复
很不错啊不错加油
wwwang89123 2014-02-12
  • 打赏
  • 举报
回复
( ^_^ )不错嘛
creso 2014-02-10
  • 打赏
  • 举报
回复

- (void)drawRect:(CGRect)rect
{
    // Drawing code
    
    if (self.image != nil && ![self.image isEqual:[NSNull null]]) {

        CGRect imgRect = originalFrame;//originalBounds;
        imgRect.origin = CGPointMake(shadowWidth * 2, shadowWidth * 2);
        CGContextRef context = UIGraphicsGetCurrentContext();
        
        UIGraphicsBeginImageContextWithOptions(self.bounds.size, NO, [CYInfo screenScale]);
        CGContextRef imgContext = UIGraphicsGetCurrentContext();
        CGPathRef clippingPath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(imgRect.size.width / 2, imgRect.size.height / 2) radius:self.cornerRadius - 2 startAngle:0.0 endAngle:360 clockwise:YES].CGPath;  //bezierPathWithRoundedRect:imgRect cornerRadius:self.cornerRadius].CGPath;
        CGContextAddPath(imgContext, clippingPath);
        CGContextClip(imgContext);
        [image drawInRect:imgRect];
        UIImage *currentContextImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        
        if (shadowWidth > 0.0) {
            CGContextSetShadowWithColor(context, shadowOffset, shadowBlur, self.shadowColor.CGColor);
        }
        [currentContextImage drawInRect:self.bounds];
    }
}
gxingmin 2014-02-10
  • 打赏
  • 举报
回复
也可以这样实现圆形相框
#import <QuartzCore/QuartzCore.h>

imageView.layer.cornerRadius = imageView.frame.size.width/2;
imageView.layer.masksToBounds = YES;
SiamPig 2014-02-10
  • 打赏
  • 举报
回复
引用 47 楼 furlxy 的回复:
用不着这么麻烦

#import <QuartzCore/QuartzCore.h>
// ...
[image.layer setCornerRadius:17];
[image.layer setMasksToBounds:TRUE];
这个方法起始挺好的,不需要那么麻烦的
whq_0413 2014-01-28
  • 打赏
  • 举报
回复
CALayer *l = [img_HeaderIcon layer]; //获取ImageView的层 [l setMasksToBounds:YES]; [l setCornerRadius:15.0]; 这三行代码就能搞定。img_HeaderIcon是你想把哪个UIIMageView来变成圆形的,15.0是根据图片的大小来改变的角度,so easy!
qq823869568 2013-04-29
  • 打赏
  • 举报
回复
sosoben 2013-04-09
  • 打赏
  • 举报
回复
引用 5 楼 u010133114 的回复:
这是C的吗,还是C++的
Objective C 上百度google一下吧
u010229958 2013-04-09
  • 打赏
  • 举报
回复
顶,楼主牛人,膜拜
xhang21a 2013-04-09
  • 打赏
  • 举报
回复
楼主牛啊,学习了
_过去已空白 2013-04-09
  • 打赏
  • 举报
回复
无满意结贴,来接分的
zxqw3366 2013-04-08
  • 打赏
  • 举报
回复
isaced 2013-04-07
  • 打赏
  • 举报
回复
不错哦!!!
u010202722 2013-04-07
  • 打赏
  • 举报
回复
furlxy 2013-04-07
  • 打赏
  • 举报
回复
用不着这么麻烦

#import <QuartzCore/QuartzCore.h>
// ...
[image.layer setCornerRadius:17];
[image.layer setMasksToBounds:TRUE];
zyr987503101 2013-04-07
  • 打赏
  • 举报
回复
看不懂,悲催的。。。。
加载更多回复(34)

29,027

社区成员

发帖
与我相关
我的任务
社区描述
主要讨论与iOS相关的软件和技术
社区管理员
  • iOS
  • 大熊猫侯佩
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧