简要
平时我们在做项目时用到UIImage的地方很多,本文对我们在做项目时最常用的做了总结,废话不多说直接上代码。
1.首先我们创建一个UIImage的分类(category)
2.在.h中的代码
#import
NS_ASSUME_NONNULL_BEGIN
@interface UIImage (LCCut)
/**
图片按照指定大小裁剪
@param asize 指定裁剪大小
@return 图片按照指定大小裁剪
*/
- (UIImage *)clipImageWithScaleWithsize:(CGSize)asize;
/**
等比例压缩,质量和大小
@param maxLength 压缩大小
@return 压缩后的图片
*/
- (UIImage *)compressWithMaxLength:(NSUInteger)maxLength;
/**
压缩得到图片二进制
@param dataSize 指定大小
@return 二进制图片
*/
- (NSData *)getImageDataWithSize:(NSInteger)dataSize;
/**
压缩图片到一定比例
@param scaleNumber 比例数0-1
@return 二进制图片
*/
- (NSData *)getImageDateWthScale:(CGFloat)scaleNumber;
/**
* 裁剪圆形
*
* @return image
*/
- (UIImage *)lc_roundImage;
/**
给图片加边框
@param borderWidth 边框宽度
@param borderColor 边框颜色
*/
- (UIImage*)lc_setRoundImageBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor;
/**
图片压缩到指定大小
@param targetSize 压缩尺寸
@param sourceImage 图片
@return 压缩后的图片
*/
+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize sourceImage:(UIImage *)sourceImage;
@end
NS_ASSUME_NONNULL_END
3.在.m中的代码
#import "UIImage+LCCut.h"
@implementation UIImage (LCCut)
/**
图片按照指定大小裁剪
@param asize 指定裁剪大小
@return 图片按照指定大小裁剪
*/
- (UIImage *)clipImageWithScaleWithsize:(CGSize)asize
{
UIImage *newimage;
UIImage *image = self;
if (nil == image)
{
newimage = nil;
}
else
{
CGSize oldsize = image.size;
CGRect rect;
if (asize.width/asize.height > oldsize.width/oldsize.height)
{
rect.size.width = asize.width;
rect.size.height = asize.width*oldsize.height/oldsize.width;
rect.origin.x = 0;
rect.origin.y = (asize.height - rect.size.height)/2;
}
else
{
rect.size.width = asize.height*oldsize.width/oldsize.height;
rect.size.height = asize.height;
rect.origin.x = (asize.width - rect.size.width)/2;
rect.origin.y = 0;
}
UIGraphicsBeginImageContext(asize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, asize.width, asize.height));
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
UIRectFill(CGRectMake(0, 0, asize.width, asize.height));
//clear background
[image drawInRect:rect];
newimage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
return newimage;
}
/**
等比例压缩,质量和大小
@param maxLength 压缩大小
@return 压缩后的图片
*/
- (UIImage *)compressWithMaxLength:(NSUInteger)maxLength {
CGFloat compression = 0.5;
NSData *data = UIImageJPEGRepresentation(self, compression);
CGFloat ratio = (CGFloat)maxLength / data.length;
if (data.length < maxLength) ratio = 1.0;
CGFloat ratioo = sqrtf(ratio);
CGFloat width = self.size.width;
CGFloat height = self.size.height;
CGSize size = CGSizeMake((NSUInteger)(width * ratioo),
(NSUInteger)(height * ratioo));
UIGraphicsBeginImageContext(size);
[self drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return resultImage;
}
/**
压缩得到图片二进制
@param dataSize 指定大小
@return 二进制图片
*/
- (NSData *)getImageDataWithSize:(NSInteger)dataSize {
UIImage *newImage = self;
NSData *data = UIImageJPEGRepresentation(newImage, 1);
//压缩图片(微信限制缩略图大小不得超过500k)
if([data length]>dataSize*1024){
CGSize size;
long factor = [data length]/(dataSize*1024);
size.width = (int)newImage.size.width/sqrt(factor);
size.height = (int)newImage.size.height/sqrt(factor);
newImage = [UIImage imageByScalingAndCroppingForSize:size sourceImage:newImage];
data = UIImageJPEGRepresentation(newImage,0.5);
}
return data;
}
/**
压缩图片到一定比例
@param scaleNumber 比例数0-1
@return 二进制图片
*/
- (NSData *)getImageDateWthScale:(CGFloat)scaleNumber {
UIImage *image = self;
NSData *data = UIImageJPEGRepresentation(image, scaleNumber);
return data;
}
/**
* 裁剪圆形
*
* @return image
*/
- (UIImage *)lc_roundImage {
CGFloat width = self.size.width;
CGFloat height = self.size.height;
// 获取图片的直径
CGFloat diameter = width > height ? height : width;
CGRect rect = CGRectMake((width - diameter) * 0.5, (height - diameter) * 0.5, diameter, diameter);
CGImageRef imageRef = CGImageCreateWithImageInRect(self.CGImage, rect);
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
rect.origin = CGPointZero;
UIGraphicsBeginImageContext(CGSizeMake(diameter, diameter));
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextAddEllipseInRect(context, rect);
CGContextClip(context);
[newImage drawInRect:rect];
UIImage *roundImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return roundImage;
}
/**
给图片加边框
@param borderWidth 边框宽度
@param borderColor 边框颜色
*/
- (UIImage*)lc_setRoundImageBorderWidth:(CGFloat)borderWidth borderColor:(UIColor *)borderColor {
UIGraphicsBeginImageContext(self.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextDrawImage(context, CGRectMake(0, 0, self.size.width, self.size.height), self.CGImage);
//圆的边框宽度为2
CGContextSetLineWidth(context,borderWidth);
CGContextSetStrokeColorWithColor(context, borderColor.CGColor);
CGFloat originX = self.size.width / 2;
CGFloat originY = self.size.height / 2;
// 计算半径
CGFloat subRadius = MIN(originX, originY) - 1;
// 逆时针画一个圆弧
CGContextAddArc(context, self.size.width / 2, self.size.height / 2, subRadius, 0, M_PI*2, 1);
CGContextStrokePath(context);
//生成新的image
UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newimg;
}
/**
图片压缩到指定大小
@param targetSize 压缩尺寸
@param sourceImage 图片
@return 压缩后的图片
*/
+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize sourceImage:(UIImage *)sourceImage {
UIImage *newImage = nil;
CGSize imageSize = sourceImage.size;
CGFloat width = imageSize.width;
CGFloat height = imageSize.height;
CGFloat targetWidth = targetSize.width;
CGFloat targetHeight = targetSize.height;
CGFloat scaleFactor = 0.0;
CGFloat scaledWidth = targetWidth;
CGFloat scaledHeight = targetHeight;
CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
if (CGSizeEqualToSize(imageSize, targetSize) == NO) {
CGFloat widthFactor = targetWidth / width;
CGFloat heightFactor = targetHeight / height;
if (widthFactor > heightFactor)
scaleFactor = widthFactor; // scale to fit height
else
scaleFactor = heightFactor; // scale to fit width
scaledWidth= width * scaleFactor;
scaledHeight = height * scaleFactor;
// center the image
if (widthFactor > heightFactor) {
thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
}
else if (widthFactor < heightFactor) {
thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
}
}
UIGraphicsBeginImageContext(targetSize); // this will crop
CGRect thumbnailRect = CGRectZero;
thumbnailRect.origin = thumbnailPoint;
thumbnailRect.size.width= scaledWidth;
thumbnailRect.size.height = scaledHeight;
[sourceImage drawInRect:thumbnailRect];
newImage = UIGraphicsGetImageFromCurrentImageContext();
if(newImage == nil)
NSLog(@"could not scale image");
//pop the context to get back to the default
UIGraphicsEndImageContext();
return newImage;
}
注:只是自己的随心笔录,文中有引用别人内容若有侵权请及时联系我