iOS 实现UIImageView的椭圆效果

Demo下载 传送门

上代码

  UIImage * srcImg =[UIImage imageNamed:@"image"];
    CGFloat width = srcImg.size.width;
    CGFloat height = srcImg.size.height;
    //开始绘制图片
    UIGraphicsBeginImageContext(srcImg.size);
    CGContextRef gc = UIGraphicsGetCurrentContext();
    ////绘制Clip区域
    //我的图片是120*160
    CGContextAddEllipseInRect(gc, CGRectMake(0, 0,120, 160)); //椭圆
    CGContextClosePath(gc);
    CGContextClip(gc);
    //坐标系转换
    //因为CGContextDrawImage会使用Quartz内的以左下角为(0,0)的坐标系
    CGContextTranslateCTM(gc, 0, height);
    CGContextScaleCTM(gc, 1, -1);
    CGContextDrawImage(gc, CGRectMake(0, 0, width, height), [srcImg CGImage]);
    //结束绘画
    UIImage *destImg = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageView * view =[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 120, 160)];
    [view setImage:destImg];
    [self.view addSubview:view];



你可能感兴趣的:(ios,graphics,core,uiimage,UIImageView)