图片的裁剪

1.将方形图片裁剪成圆形

-(void)testImageClip

{

    //加载图片

    UIImage *img = [UIImage imageNamed:@"123"];

    

    //开启上下文,跟图片尺寸一样大

    UIGraphicsBeginImageContextWithOptions(img.size, NO, 0);

    

    //设置裁剪区域

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, img.size.width, img.size.height)];

    [path addClip];

    

    //绘制图片

    [img drawAtPoint:CGPointZero];

    

    //从上下文获取图片

    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();

    

    //关闭上下文

    UIGraphicsEndImageContext();

    

    _imageV.image = clipImage;

    

}

2.给圆形再添加个边框

@implementation UIImage (Clip)

+(UIImage*)imageWithClipImage:(UIImage *)image borderColor:(UIColor *)color borderWidth:(CGFloat)borderWidth

{

    CGFloat imageWH = image.size.height;

    

    CGFloat border = borderWidth;

    

    CGFloat ovaWH = imageWH ;

    

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(ovaWH, ovaWH), NO, 0  );

    

    UIBezierPath *path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, ovaWH, ovaWH)];

    [color set];

    [path fill];

    //设置裁剪区域

    UIBezierPath *clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(border, border, imageWH - 2*border, imageWH - 2*border)];

    [clipPath addClip];

    

    //绘制图片

    [image drawAtPoint:CGPointZero];

    

    //从上下文获取图片

    UIImage *clipImage = UIGraphicsGetImageFromCurrentImageContext();

    

    //关闭上下文

    UIGraphicsEndImageContext();

    

    return clipImage;

    

}

@end

 将图片按照原来的宽高比例压缩到与窗口合适的大小,然后在设置了_imageView.contentMode = UIViewContentModeCenter;  这个属性的UIImageView中展示压缩后的图片。

//压缩图片  

- (UIImage *)image:(UIImage*)image scaledToSize:(CGSize)newSize  

{  

    // Create a graphics image context  

    UIGraphicsBeginImageContext(newSize);  

    // Tell the old image to draw in this new context, with the desired  

    // new size  

    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];  

    // Get the new image from the context  

    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();  

    // End the context  

    UIGraphicsEndImageContext();  

    // Return the new image.  

    return newImage;  

}  

按照窗口宽高比例,将原图横向或者纵向裁剪掉多余的部分,然后不设置UIImageView的contentMode属性,将裁剪后的图片送进去,使其自动适应窗口。

//裁剪图片  

- (UIImage *)cutImage:(UIImage*)image  

{  

//压缩图片  

    CGSize newSize;  

    CGImageRef imageRef = nil;  

if ((image.size.width / image.size.height) < (bgImgView.size.width / bgImgView.size.height)) {  

        newSize.width = image.size.width;  

        newSize.height = image.size.width *bgImgView.size.height /bgImgView.size.width;  

        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(0, fabs(image.size.height - newSize.height) / 2, newSize.width, newSize.height));  

    } else {  

        newSize.height = image.size.height;  

        newSize.width = image.size.height *bgImgView.size.width / bgImgView.size.height;  

        imageRef = CGImageCreateWithImageInRect([image CGImage], CGRectMake(fabs(image.size.width - newSize.width) / 2, 0, newSize.width, newSize.height));  

    }  

return [UIImage imageWithCGImage:imageRef];  

}  

 

利用层裁剪

@property (weak, nonatomic) IBOutlet UIImageView *imageV;

   _imageV.image = img;

   

_imageV.layer.masksToBounds = YES;

    _imageV.layer.cornerRadius = img.size.width*0.5;

    _imageV.layer.borderWidth = 5;

    _imageV.layer.borderColor = [[UIColor redColor] CGColor];

原文地址:https://www.cnblogs.com/PJXWang/p/5623616.html