123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447 |
- #import "UIImage+Extension.h"
- #import <ImageIO/ImageIO.h>
- @implementation UIImage (Extension)
- + (UIImage *)imageFromURLString: (NSString *)urlString {
- NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- - (UIImage *)resizeImage:(CGSize)resize {
- // DDLogInfo(@"%@ originalSize: %@, expectedSize: %@", NSStringFromSelector(_cmd), NSStringFromCGSize(self.size), NSStringFromCGSize(expectedSize));
-
- CGFloat totalExpectedSize = resize.height * resize.width;
-
- if (totalExpectedSize < self.size.height * self.size.width) {
-
- CGFloat ratio;
- if (self.size.width < self.size.height) {
- resize = CGSizeMake(resize.height, resize.width);
- ratio = self.size.width / resize.width;
- }
- else if (self.size.height < self.size.width) {
- ratio = self.size.height / resize.height;
- }
- else {
- ratio = self.size.height / MIN(resize.height, resize.width);
- }
-
- CGSize newSize = CGSizeMake(self.size.width / ratio, self.size.height / ratio);
-
- // DDLogInfo(@"%@ outputSize: %@", NSStringFromSelector(_cmd), NSStringFromCGSize(newSize));
-
- UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
- [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return newImage;
- }
-
- return self;
- }
- + (UIImage*)resizableImage:(NSString *)name
- {
- UIImage *normal = [UIImage imageNamed:name];
-
- CGFloat imageW = normal.size.width * 0.5;
- CGFloat imageH = normal.size.height * 0.5;
- return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(imageH, imageW, imageH, imageW)];
- }
- /**
- * 压缩上传图片到指定字节
- *
- * @param image 压缩的图片
- * @param maxLength 压缩后最大字节大小
- *
- * @return 压缩后图片的二进制
- */
- + (NSData *)compressImage:(UIImage *)image toMaxLength:(NSInteger)maxLength maxWidth:(NSInteger)maxWidth{
- NSAssert(maxLength > 0, @"图片的大小必须大于 0");
- NSAssert(maxWidth > 0, @"图片的最大边长必须大于 0");
-
- CGSize newSize = [self scaleImage:image withLength:maxWidth];
- UIImage *newImage = [self resizeImage:image withNewSize:newSize];
-
- CGFloat compress = 0.9f;
- NSData *data = UIImageJPEGRepresentation(newImage, compress);
-
- while (data.length > maxLength && compress > 0.01) {
- compress -= 0.02f;
-
- data = UIImageJPEGRepresentation(newImage, compress);
- }
- return data;
- }
- /**
- * 获得指定size的图片
- *
- * @param image 原始图片
- * @param newSize 指定的size
- *
- * @return 调整后的图片
- */
- + (UIImage *)resizeImage:(UIImage *) image withNewSize:(CGSize) newSize{
-
- UIGraphicsBeginImageContext(newSize);
- [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
-
- UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
-
- return newImage;
- }
- /**
- * 通过指定图片最长边,获得等比例的图片size
- *
- * @param image 原始图片
- * @param imageLength 图片允许的最长宽度(高度)
- *
- * @return 获得等比例的size
- */
- + (CGSize)scaleImage:(UIImage *) image withLength:(CGFloat) imageLength{
-
- CGFloat newWidth = 0.0f;
- CGFloat newHeight = 0.0f;
- CGFloat width = image.size.width;
- CGFloat height = image.size.height;
-
- if (width > imageLength || height > imageLength){
-
- if (width > height) {
-
- newWidth = imageLength;
- newHeight = newWidth * height / width;
-
- }else if(height > width){
-
- newHeight = imageLength;
- newWidth = newHeight * width / height;
-
- }else{
-
- newWidth = imageLength;
- newHeight = imageLength;
- }
-
- }else{
- return CGSizeMake(width, height);
- }
-
- return CGSizeMake(newWidth, newHeight);
- }
- + (UIImage *)resizedImageWithName:(NSString *)name
- {
- UIImage *image = [self imageNamed:name];
- return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
- }
- ///居中裁剪
- - (void)CenterClipImageView:(UIImageView*)imageView {
- [imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
- imageView.contentMode = UIViewContentModeScaleAspectFill;
- imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
- imageView.clipsToBounds = YES;
- }
- //颜色转换成图片
- + (UIImage *)xlsn0w_imageWithColor:(UIColor *)color {
- CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
- UIGraphicsBeginImageContext(rect.size);
- CGContextRef context = UIGraphicsGetCurrentContext();
- CGContextSetFillColorWithColor(context, [color CGColor]);
- CGContextFillRect(context, rect);
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
- - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
- // 创建一个bitmap的context
- // 并把它设置成为当前正在使用的context
- UIGraphicsBeginImageContext(size);
- // 绘制改变大小的图片
- [img drawInRect:CGRectMake(0,0, size.width, size.height)];
- // 从当前context中创建一个改变大小后的图片
- UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
- // 使当前的context出堆栈
- UIGraphicsEndImageContext();
- //返回新的改变大小后的图片
- return scaledImage;
- }
- /* 图片压缩到指定大小
- * image 要压缩的图片
- * apSize 压缩的制定尺寸 如:CGSizeMake(1440, 1080)
- *
- */
- +(UIImage*)imageByScalingAndCroppingForSize:(UIImage *)image appointSize:(CGSize )apSize{
- CGSize targetSize;
- float ratio;
- NSInteger multiplem;
- if (image.size.width > image.size.height && image.size.width >apSize.width) {
- ratio = image.size.width/ apSize.height;
- multiplem = (int)ceilf(ratio);
- } else if (image.size.width < image.size.height && image.size.height >apSize.height) {
- ratio = image.size.height/apSize.height;
- multiplem = (int)ceilf(ratio);
- } else if (image.size.width == image.size.height && image.size.height >apSize.height) {
- ratio = image.size.height/apSize.height;
- multiplem = (int)ceilf(ratio);
- } else {
- return image;
- }
- targetSize = CGSizeMake(image.size.width/multiplem, image.size.height/multiplem);
- UIImage *sourceImage = image;
- 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;
- 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)
- UIGraphicsEndImageContext();
- return newImage;
- return nil;
- }
- - (UIImage*)TransformtoSize:(CGSize)size {
- UIGraphicsBeginImageContext(size);
- // 绘制改变大小的图片
- CGRect rect = CGRectMake(0, 0, size.width, size.height);
- [self drawInRect:rect];
-
- //从当前context中创建一个改变大小后的图片
- UIImage *TransformedImg = UIGraphicsGetImageFromCurrentImageContext();
-
- UIGraphicsEndImageContext();
-
- return TransformedImg;
- }
- //等比例压缩
- + (UIImage *)imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
- UIImage *newImage = nil;
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
- CGFloat targetWidth = size.width;
- CGFloat targetHeight = size.height;
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
- if(CGSizeEqualToSize(imageSize, size) == NO){
- CGFloat widthFactor = targetWidth / width;
- CGFloat heightFactor = targetHeight / height;
- if(widthFactor > heightFactor){
- scaleFactor = widthFactor;
- }
- else{
- scaleFactor = heightFactor;
- }
- scaledWidth = width * scaleFactor;
- scaledHeight = height * scaleFactor;
- if(widthFactor > heightFactor){
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- }else if(widthFactor < heightFactor){
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
-
- UIGraphicsBeginImageContext(size);
-
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
- [sourceImage drawInRect:thumbnailRect];
- newImage = UIGraphicsGetImageFromCurrentImageContext();
-
- if(newImage == nil){
- NSLog(@"scale image fail");
- }
-
- UIGraphicsEndImageContext();
-
- return newImage;
-
- }
- - (UIImage *)imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
- UIImage *newImage = nil;
- CGSize imageSize = sourceImage.size;
- CGFloat width = imageSize.width;
- CGFloat height = imageSize.height;
- CGFloat targetWidth = defineWidth;
- CGFloat targetHeight = height / (width / targetWidth);
- CGSize size = CGSizeMake(targetWidth, targetHeight);
- CGFloat scaleFactor = 0.0;
- CGFloat scaledWidth = targetWidth;
- CGFloat scaledHeight = targetHeight;
- CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
- if(CGSizeEqualToSize(imageSize, size) == NO){
- CGFloat widthFactor = targetWidth / width;
- CGFloat heightFactor = targetHeight / height;
- if(widthFactor > heightFactor){
- scaleFactor = widthFactor;
- }
- else{
- scaleFactor = heightFactor;
- }
- scaledWidth = width * scaleFactor;
- scaledHeight = height * scaleFactor;
- if(widthFactor > heightFactor){
- thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
- }else if(widthFactor < heightFactor){
- thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
- }
- }
- UIGraphicsBeginImageContext(size);
- CGRect thumbnailRect = CGRectZero;
- thumbnailRect.origin = thumbnailPoint;
- thumbnailRect.size.width = scaledWidth;
- thumbnailRect.size.height = scaledHeight;
-
- [sourceImage drawInRect:thumbnailRect];
-
- newImage = UIGraphicsGetImageFromCurrentImageContext();
- if(newImage == nil){
- NSLog(@"scale image fail");
- }
-
- UIGraphicsEndImageContext();
- return newImage;
- }
- /**
- * 拉伸图片
- *
- * @param imageName imageName description
- *
- * @return <#return value description#>
- */
- //+ (UIImage *)resizableImage:(NSString *)imageName
- //{
- // UIImage *image = [UIImage imageNamed:imageName];
- //
- // return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
- //}
- /**
- * 拉伸图片
- */
- + (UIImage *)imageWithResize:(NSString *)imageName left:(CGFloat)left top:(CGFloat)top
- {
- UIImage * image = [UIImage imageNamed:imageName];
- CGFloat imageHB = (image.size.height - 1) * top;
- CGFloat imageLR = (image.size.width - 1) * left;
- return [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHB, imageLR, imageHB, imageLR)];
- }
- /**
- * 拉伸图片
- */
- + (UIImage *)imageWithResize:(NSString *)imageName
- {
- return [self imageWithResize:imageName left:0.5 top:0.5];
- }
- /**
- * 加载图片
- */
- + (UIImage *)imageWithName:(NSString *)name
- {
- NSString * newName = [name stringByAppendingString:@"_os7"];
-
- UIImage * image = [UIImage imageNamed:newName];
- if (!image) {
- image = [UIImage imageNamed:name];
- }
- return image;
-
- }
- + (CGSize)getSizeFromURL:(id)URL {
- NSURL * url = nil;
- if ([URL isKindOfClass:[NSURL class]]) {
- url = URL;
- }
- if ([URL isKindOfClass:[NSString class]]) {
- url = [NSURL URLWithString:URL];
- }
- if (!URL) {
- return CGSizeZero;
- }
- CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
- CGFloat width = 0, height = 0;
- if (imageSourceRef) {
- CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
- //以下是对手机32位、64位的处理
- if (imageProperties != NULL) {
- CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
- #if defined(__LP64__) && __LP64__
- if (widthNumberRef != NULL) {
- CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
- }
- CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
- if (heightNumberRef != NULL) {
- CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
- }
- #else
- if (widthNumberRef != NULL) {
- CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width);
- }
- CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
- if (heightNumberRef != NULL) {
- CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height);
- }
- #endif
- CFRelease(imageProperties);
- }
-
- CFRelease(imageSourceRef);
- }
- return CGSizeMake(width, height);
- }
- @end
|