// // UIImage+RQExtension.m // RQCommon // // Created by 张嵘 on 2018/11/16. // Copyright © 2018 张嵘. All rights reserved. // #import "UIImage+RQExtension.h" #import #import @implementation UIImage (RQExtension) /** * 根据图片名返回一张能够自由拉伸的图片 (从中间拉伸) */ + (UIImage *)rq_resizableImage:(NSString *)imgName { UIImage *image = [UIImage imageNamed:imgName]; return [self rq_resizableImage:imgName capInsets:UIEdgeInsetsMake(image.size.height *.5f, image.size.width*.5f, image.size.height*.5f, image.size.width*.5f)]; } + (UIImage *)rq_resizableImage:(NSString *)imgName capInsets:(UIEdgeInsets)capInsets { UIImage *image = [UIImage imageNamed:imgName]; return [image resizableImageWithCapInsets:capInsets]; } + (UIImage *)rq_imageAlwaysShowOriginalImageWithImageName:(NSString *)imageName { UIImage *image = [UIImage imageNamed:imageName]; if ([image respondsToSelector:@selector(imageWithRenderingMode:)]) { //iOS 7.0+ return [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]; }else{ return image; } } + (UIImage*)rq_thumbnailImageForVideo:(NSURL *)videoURL atTime:(NSTimeInterval)time { AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil]; NSParameterAssert(asset); AVAssetImageGenerator *assetImageGenerator =[[AVAssetImageGenerator alloc] initWithAsset:asset]; assetImageGenerator.appliesPreferredTrackTransform = YES; assetImageGenerator.apertureMode = AVAssetImageGeneratorApertureModeEncodedPixels; CGImageRef thumbnailImageRef = NULL; CFTimeInterval thumbnailImageTime = time; NSError *thumbnailImageGenerationError = nil; thumbnailImageRef = [assetImageGenerator copyCGImageAtTime:CMTimeMake(thumbnailImageTime, 60)actualTime:NULL error:&thumbnailImageGenerationError]; if(!thumbnailImageRef) NSLog(@"thumbnailImageGenerationError %@",thumbnailImageGenerationError); UIImage*thumbnailImage = thumbnailImageRef ? [[UIImage alloc]initWithCGImage: thumbnailImageRef] : nil; return thumbnailImage; } /// 获取屏幕截图 /// /// @return 屏幕截图图像 + (UIImage *)rq_screenShot { // 1. 获取到窗口 UIWindow *window = [UIApplication sharedApplication].keyWindow; // 2. 开始上下文 UIGraphicsBeginImageContextWithOptions(window.bounds.size, YES, 0); // 3. 将 window 中的内容绘制输出到当前上下文 [window drawViewHierarchyInRect:window.bounds afterScreenUpdates:NO]; // 4. 获取图片 UIImage *screenShot = UIGraphicsGetImageFromCurrentImageContext(); // 5. 关闭上下文 UIGraphicsEndImageContext(); return screenShot; } - (UIImage *)rq_fixOrientation { // No-op if the orientation is already correct if (self.imageOrientation == UIImageOrientationUp) return self; // We need to calculate the proper transformation to make the image upright. // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored. CGAffineTransform transform = CGAffineTransformIdentity; switch (self.imageOrientation) { case UIImageOrientationDown: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height); transform = CGAffineTransformRotate(transform, M_PI); break; case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformRotate(transform, M_PI_2); break; case UIImageOrientationRight: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, 0, self.size.height); transform = CGAffineTransformRotate(transform, -M_PI_2); break; case UIImageOrientationUp: case UIImageOrientationUpMirrored: break; } switch (self.imageOrientation) { case UIImageOrientationUpMirrored: case UIImageOrientationDownMirrored: transform = CGAffineTransformTranslate(transform, self.size.width, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationLeftMirrored: case UIImageOrientationRightMirrored: transform = CGAffineTransformTranslate(transform, self.size.height, 0); transform = CGAffineTransformScale(transform, -1, 1); break; case UIImageOrientationUp: case UIImageOrientationDown: case UIImageOrientationLeft: case UIImageOrientationRight: break; } // Now we draw the underlying CGImage into a new context, applying the transform // calculated above. CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height, CGImageGetBitsPerComponent(self.CGImage), 0, CGImageGetColorSpace(self.CGImage), CGImageGetBitmapInfo(self.CGImage)); CGContextConcatCTM(ctx, transform); switch (self.imageOrientation) { case UIImageOrientationLeft: case UIImageOrientationLeftMirrored: case UIImageOrientationRight: case UIImageOrientationRightMirrored: // Grr... CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage); break; default: CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage); break; } // And now we just create a new UIImage from the drawing context CGImageRef cgimg = CGBitmapContextCreateImage(ctx); UIImage *img = [UIImage imageWithCGImage:cgimg]; CGContextRelease(ctx); CGImageRelease(cgimg); return img; } //将JPEG格式转换为PNG -(UIImage *)reduceImage:(UIImage *)image percent:(float)percent { NSData *imageData = UIImageJPEGRepresentation(image, percent); UIImage *newImage = [UIImage imageWithData:imageData]; return newImage; } //压缩图片尺寸 - (UIImage *)scaledToSize:(CGSize)newSize { //首先要找到缩放比 按长的适配 不足的部分空白 CGFloat rate =newSize.width*1.0/ self.size.width ; if (self.size.height* rate > newSize.height) { //过长了。 rate =newSize.height*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); // Create a graphics image context UIGraphicsBeginImageContext(size); // new size [self drawInRect:CGRectMake(0,0,size.width,size.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; } - (UIImage *)scaledAndCutToSize:(CGSize)newSize{ //首先要找到缩放比 按短的适配 长的部分裁减掉 CGFloat rate =newSize.width*1.0/ self.size.width ; if (self.size.height* rate < newSize.height) { //过长了。 rate =newSize.height*1.0/ self.size.height ; } CGSize size = CGSizeMake(self.size.width*rate, self.size.height*rate); UIGraphicsBeginImageContext(size); [self drawInRect:CGRectMake(0,0,size.width,size.height)]; // Get the new image from the context UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); // End the context UIGraphicsEndImageContext(); // Return the new image. return newImage; } - (UIImage*)imageRotatedByDegrees:(CGFloat)degrees { CGFloat width = CGImageGetWidth(self.CGImage); CGFloat height = CGImageGetHeight(self.CGImage); CGSize rotatedSize; rotatedSize.width = width; rotatedSize.height = height; UIGraphicsBeginImageContext(rotatedSize); CGContextRef bitmap = UIGraphicsGetCurrentContext(); CGContextTranslateCTM(bitmap, rotatedSize.width/2, rotatedSize.height/2); CGContextRotateCTM(bitmap, degrees * M_PI / 180); CGContextRotateCTM(bitmap, M_PI); CGContextScaleCTM(bitmap, -1.0, 1.0); CGContextDrawImage(bitmap, CGRectMake(-rotatedSize.width/2, -rotatedSize.height/2, rotatedSize.width, rotatedSize.height), self.CGImage); UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } + (UIImage *)compressImageWith:(UIImage *)image { float imageWidth = image.size.width; float imageHeight = image.size.height; float width = 640; float height = image.size.height/(image.size.width/width); float widthScale = imageWidth /width; float heightScale = imageHeight /height; // 创建一个bitmap的context // 并把它设置成为当前正在使用的context UIGraphicsBeginImageContext(CGSizeMake(width, height)); if (widthScale > heightScale) { [image drawInRect:CGRectMake(0, 0, imageWidth /heightScale , height)]; } else { [image drawInRect:CGRectMake(0, 0, width , imageHeight /widthScale)]; } // 从当前context中创建一个改变大小后的图片 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); // 使当前的context出堆栈 UIGraphicsEndImageContext(); return newImage; } /** 获取网络图片高度 */ + (CGSize)getImageSizeWithURL:(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 /***************** 此处解决返回图片宽高相反问题 *****************/ // 图像旋转的方向属性 NSInteger orientation = [(__bridge NSNumber *)CFDictionaryGetValue(imageProperties, kCGImagePropertyOrientation) integerValue]; CGFloat temp = 0; switch (orientation) { // 如果图像的方向不是正的,则宽高互换 case UIImageOrientationLeft: // 向左逆时针旋转90度 case UIImageOrientationRight: // 向右顺时针旋转90度 case UIImageOrientationLeftMirrored: // 在水平翻转之后向左逆时针旋转90度 case UIImageOrientationRightMirrored: { // 在水平翻转之后向右顺时针旋转90度 temp = width; width = height; height = temp; } break; default: break; } /***************** 此处解决返回图片宽高相反问题 *****************/ CFRelease(imageProperties); } CFRelease(imageSourceRef); } return CGSizeMake(width, height); } @end