UIImage+Extension.m 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. #import "UIImage+Extension.h"
  2. #import <ImageIO/ImageIO.h>
  3. @implementation UIImage (Extension)
  4. + (UIImage *)imageFromURLString: (NSString *)urlString {
  5. NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:urlString]];
  6. UIImage *image = [UIImage imageWithData:data];
  7. return image;
  8. }
  9. - (UIImage *)resizeImage:(CGSize)resize {
  10. // DDLogInfo(@"%@ originalSize: %@, expectedSize: %@", NSStringFromSelector(_cmd), NSStringFromCGSize(self.size), NSStringFromCGSize(expectedSize));
  11. CGFloat totalExpectedSize = resize.height * resize.width;
  12. if (totalExpectedSize < self.size.height * self.size.width) {
  13. CGFloat ratio;
  14. if (self.size.width < self.size.height) {
  15. resize = CGSizeMake(resize.height, resize.width);
  16. ratio = self.size.width / resize.width;
  17. }
  18. else if (self.size.height < self.size.width) {
  19. ratio = self.size.height / resize.height;
  20. }
  21. else {
  22. ratio = self.size.height / MIN(resize.height, resize.width);
  23. }
  24. CGSize newSize = CGSizeMake(self.size.width / ratio, self.size.height / ratio);
  25. // DDLogInfo(@"%@ outputSize: %@", NSStringFromSelector(_cmd), NSStringFromCGSize(newSize));
  26. UIGraphicsBeginImageContextWithOptions(newSize, NO, 0);
  27. [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  28. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  29. UIGraphicsEndImageContext();
  30. return newImage;
  31. }
  32. return self;
  33. }
  34. + (UIImage*)resizableImage:(NSString *)name
  35. {
  36. UIImage *normal = [UIImage imageNamed:name];
  37. CGFloat imageW = normal.size.width * 0.5;
  38. CGFloat imageH = normal.size.height * 0.5;
  39. return [normal resizableImageWithCapInsets:UIEdgeInsetsMake(imageH, imageW, imageH, imageW)];
  40. }
  41. /**
  42. * 压缩上传图片到指定字节
  43. *
  44. * @param image 压缩的图片
  45. * @param maxLength 压缩后最大字节大小
  46. *
  47. * @return 压缩后图片的二进制
  48. */
  49. + (NSData *)compressImage:(UIImage *)image toMaxLength:(NSInteger)maxLength maxWidth:(NSInteger)maxWidth{
  50. NSAssert(maxLength > 0, @"图片的大小必须大于 0");
  51. NSAssert(maxWidth > 0, @"图片的最大边长必须大于 0");
  52. CGSize newSize = [self scaleImage:image withLength:maxWidth];
  53. UIImage *newImage = [self resizeImage:image withNewSize:newSize];
  54. CGFloat compress = 0.9f;
  55. NSData *data = UIImageJPEGRepresentation(newImage, compress);
  56. while (data.length > maxLength && compress > 0.01) {
  57. compress -= 0.02f;
  58. data = UIImageJPEGRepresentation(newImage, compress);
  59. }
  60. return data;
  61. }
  62. /**
  63. * 获得指定size的图片
  64. *
  65. * @param image 原始图片
  66. * @param newSize 指定的size
  67. *
  68. * @return 调整后的图片
  69. */
  70. + (UIImage *)resizeImage:(UIImage *) image withNewSize:(CGSize) newSize{
  71. UIGraphicsBeginImageContext(newSize);
  72. [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
  73. UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
  74. UIGraphicsEndImageContext();
  75. return newImage;
  76. }
  77. /**
  78. * 通过指定图片最长边,获得等比例的图片size
  79. *
  80. * @param image 原始图片
  81. * @param imageLength 图片允许的最长宽度(高度)
  82. *
  83. * @return 获得等比例的size
  84. */
  85. + (CGSize)scaleImage:(UIImage *) image withLength:(CGFloat) imageLength{
  86. CGFloat newWidth = 0.0f;
  87. CGFloat newHeight = 0.0f;
  88. CGFloat width = image.size.width;
  89. CGFloat height = image.size.height;
  90. if (width > imageLength || height > imageLength){
  91. if (width > height) {
  92. newWidth = imageLength;
  93. newHeight = newWidth * height / width;
  94. }else if(height > width){
  95. newHeight = imageLength;
  96. newWidth = newHeight * width / height;
  97. }else{
  98. newWidth = imageLength;
  99. newHeight = imageLength;
  100. }
  101. }else{
  102. return CGSizeMake(width, height);
  103. }
  104. return CGSizeMake(newWidth, newHeight);
  105. }
  106. + (UIImage *)resizedImageWithName:(NSString *)name
  107. {
  108. UIImage *image = [self imageNamed:name];
  109. return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
  110. }
  111. ///居中裁剪
  112. - (void)CenterClipImageView:(UIImageView*)imageView {
  113. [imageView setContentScaleFactor:[[UIScreen mainScreen] scale]];
  114. imageView.contentMode = UIViewContentModeScaleAspectFill;
  115. imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;
  116. imageView.clipsToBounds = YES;
  117. }
  118. //颜色转换成图片
  119. + (UIImage *)xlsn0w_imageWithColor:(UIColor *)color {
  120. CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
  121. UIGraphicsBeginImageContext(rect.size);
  122. CGContextRef context = UIGraphicsGetCurrentContext();
  123. CGContextSetFillColorWithColor(context, [color CGColor]);
  124. CGContextFillRect(context, rect);
  125. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  126. UIGraphicsEndImageContext();
  127. return image;
  128. }
  129. - (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
  130. // 创建一个bitmap的context
  131. // 并把它设置成为当前正在使用的context
  132. UIGraphicsBeginImageContext(size);
  133. // 绘制改变大小的图片
  134. [img drawInRect:CGRectMake(0,0, size.width, size.height)];
  135. // 从当前context中创建一个改变大小后的图片
  136. UIImage* scaledImage =UIGraphicsGetImageFromCurrentImageContext();
  137. // 使当前的context出堆栈
  138. UIGraphicsEndImageContext();
  139. //返回新的改变大小后的图片
  140. return scaledImage;
  141. }
  142. /* 图片压缩到指定大小
  143. * image 要压缩的图片
  144. * apSize 压缩的制定尺寸 如:CGSizeMake(1440, 1080)
  145. *
  146. */
  147. +(UIImage*)imageByScalingAndCroppingForSize:(UIImage *)image appointSize:(CGSize )apSize{
  148. CGSize targetSize;
  149. float ratio;
  150. NSInteger multiplem;
  151. if (image.size.width > image.size.height && image.size.width >apSize.width) {
  152. ratio = image.size.width/ apSize.height;
  153. multiplem = (int)ceilf(ratio);
  154. } else if (image.size.width < image.size.height && image.size.height >apSize.height) {
  155. ratio = image.size.height/apSize.height;
  156. multiplem = (int)ceilf(ratio);
  157. } else if (image.size.width == image.size.height && image.size.height >apSize.height) {
  158. ratio = image.size.height/apSize.height;
  159. multiplem = (int)ceilf(ratio);
  160. } else {
  161. return image;
  162. }
  163. targetSize = CGSizeMake(image.size.width/multiplem, image.size.height/multiplem);
  164. UIImage *sourceImage = image;
  165. UIImage *newImage = nil;
  166. CGSize imageSize = sourceImage.size;
  167. CGFloat width = imageSize.width;
  168. CGFloat height = imageSize.height;
  169. CGFloat targetWidth = targetSize.width;
  170. CGFloat targetHeight = targetSize.height;
  171. CGFloat scaleFactor = 0.0;
  172. CGFloat scaledWidth = targetWidth;
  173. CGFloat scaledHeight = targetHeight;
  174. CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
  175. if (CGSizeEqualToSize(imageSize, targetSize) == NO)
  176. {
  177. CGFloat widthFactor = targetWidth / width;
  178. CGFloat heightFactor = targetHeight / height;
  179. if (widthFactor > heightFactor)
  180. scaleFactor = widthFactor; // scale to fit height
  181. else
  182. scaleFactor = heightFactor; // scale to fit width
  183. scaledWidth= width * scaleFactor;
  184. scaledHeight = height * scaleFactor;
  185. if (widthFactor > heightFactor){
  186. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  187. }else if (widthFactor < heightFactor){
  188. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  189. }
  190. }
  191. UIGraphicsBeginImageContext(targetSize); // this will crop
  192. CGRect thumbnailRect = CGRectZero;
  193. thumbnailRect.origin = thumbnailPoint;
  194. thumbnailRect.size.width= scaledWidth;
  195. thumbnailRect.size.height = scaledHeight;
  196. [sourceImage drawInRect:thumbnailRect];
  197. newImage = UIGraphicsGetImageFromCurrentImageContext();
  198. if(newImage == nil)
  199. UIGraphicsEndImageContext();
  200. return newImage;
  201. return nil;
  202. }
  203. - (UIImage*)TransformtoSize:(CGSize)size {
  204. UIGraphicsBeginImageContext(size);
  205. // 绘制改变大小的图片
  206. CGRect rect = CGRectMake(0, 0, size.width, size.height);
  207. [self drawInRect:rect];
  208. //从当前context中创建一个改变大小后的图片
  209. UIImage *TransformedImg = UIGraphicsGetImageFromCurrentImageContext();
  210. UIGraphicsEndImageContext();
  211. return TransformedImg;
  212. }
  213. //等比例压缩
  214. + (UIImage *)imageCompressForSize:(UIImage *)sourceImage targetSize:(CGSize)size{
  215. UIImage *newImage = nil;
  216. CGSize imageSize = sourceImage.size;
  217. CGFloat width = imageSize.width;
  218. CGFloat height = imageSize.height;
  219. CGFloat targetWidth = size.width;
  220. CGFloat targetHeight = size.height;
  221. CGFloat scaleFactor = 0.0;
  222. CGFloat scaledWidth = targetWidth;
  223. CGFloat scaledHeight = targetHeight;
  224. CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
  225. if(CGSizeEqualToSize(imageSize, size) == NO){
  226. CGFloat widthFactor = targetWidth / width;
  227. CGFloat heightFactor = targetHeight / height;
  228. if(widthFactor > heightFactor){
  229. scaleFactor = widthFactor;
  230. }
  231. else{
  232. scaleFactor = heightFactor;
  233. }
  234. scaledWidth = width * scaleFactor;
  235. scaledHeight = height * scaleFactor;
  236. if(widthFactor > heightFactor){
  237. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  238. }else if(widthFactor < heightFactor){
  239. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  240. }
  241. }
  242. UIGraphicsBeginImageContext(size);
  243. CGRect thumbnailRect = CGRectZero;
  244. thumbnailRect.origin = thumbnailPoint;
  245. thumbnailRect.size.width = scaledWidth;
  246. thumbnailRect.size.height = scaledHeight;
  247. [sourceImage drawInRect:thumbnailRect];
  248. newImage = UIGraphicsGetImageFromCurrentImageContext();
  249. if(newImage == nil){
  250. NSLog(@"scale image fail");
  251. }
  252. UIGraphicsEndImageContext();
  253. return newImage;
  254. }
  255. - (UIImage *)imageCompressForWidth:(UIImage *)sourceImage targetWidth:(CGFloat)defineWidth{
  256. UIImage *newImage = nil;
  257. CGSize imageSize = sourceImage.size;
  258. CGFloat width = imageSize.width;
  259. CGFloat height = imageSize.height;
  260. CGFloat targetWidth = defineWidth;
  261. CGFloat targetHeight = height / (width / targetWidth);
  262. CGSize size = CGSizeMake(targetWidth, targetHeight);
  263. CGFloat scaleFactor = 0.0;
  264. CGFloat scaledWidth = targetWidth;
  265. CGFloat scaledHeight = targetHeight;
  266. CGPoint thumbnailPoint = CGPointMake(0.0, 0.0);
  267. if(CGSizeEqualToSize(imageSize, size) == NO){
  268. CGFloat widthFactor = targetWidth / width;
  269. CGFloat heightFactor = targetHeight / height;
  270. if(widthFactor > heightFactor){
  271. scaleFactor = widthFactor;
  272. }
  273. else{
  274. scaleFactor = heightFactor;
  275. }
  276. scaledWidth = width * scaleFactor;
  277. scaledHeight = height * scaleFactor;
  278. if(widthFactor > heightFactor){
  279. thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
  280. }else if(widthFactor < heightFactor){
  281. thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
  282. }
  283. }
  284. UIGraphicsBeginImageContext(size);
  285. CGRect thumbnailRect = CGRectZero;
  286. thumbnailRect.origin = thumbnailPoint;
  287. thumbnailRect.size.width = scaledWidth;
  288. thumbnailRect.size.height = scaledHeight;
  289. [sourceImage drawInRect:thumbnailRect];
  290. newImage = UIGraphicsGetImageFromCurrentImageContext();
  291. if(newImage == nil){
  292. NSLog(@"scale image fail");
  293. }
  294. UIGraphicsEndImageContext();
  295. return newImage;
  296. }
  297. /**
  298. * 拉伸图片
  299. *
  300. * @param imageName imageName description
  301. *
  302. * @return <#return value description#>
  303. */
  304. //+ (UIImage *)resizableImage:(NSString *)imageName
  305. //{
  306. // UIImage *image = [UIImage imageNamed:imageName];
  307. //
  308. // return [image stretchableImageWithLeftCapWidth:image.size.width * 0.5 topCapHeight:image.size.height * 0.5];
  309. //}
  310. /**
  311. * 拉伸图片
  312. */
  313. + (UIImage *)imageWithResize:(NSString *)imageName left:(CGFloat)left top:(CGFloat)top
  314. {
  315. UIImage * image = [UIImage imageNamed:imageName];
  316. CGFloat imageHB = (image.size.height - 1) * top;
  317. CGFloat imageLR = (image.size.width - 1) * left;
  318. return [image resizableImageWithCapInsets:UIEdgeInsetsMake(imageHB, imageLR, imageHB, imageLR)];
  319. }
  320. /**
  321. * 拉伸图片
  322. */
  323. + (UIImage *)imageWithResize:(NSString *)imageName
  324. {
  325. return [self imageWithResize:imageName left:0.5 top:0.5];
  326. }
  327. /**
  328. * 加载图片
  329. */
  330. + (UIImage *)imageWithName:(NSString *)name
  331. {
  332. NSString * newName = [name stringByAppendingString:@"_os7"];
  333. UIImage * image = [UIImage imageNamed:newName];
  334. if (!image) {
  335. image = [UIImage imageNamed:name];
  336. }
  337. return image;
  338. }
  339. + (CGSize)getSizeFromURL:(id)URL {
  340. NSURL * url = nil;
  341. if ([URL isKindOfClass:[NSURL class]]) {
  342. url = URL;
  343. }
  344. if ([URL isKindOfClass:[NSString class]]) {
  345. url = [NSURL URLWithString:URL];
  346. }
  347. if (!URL) {
  348. return CGSizeZero;
  349. }
  350. CGImageSourceRef imageSourceRef = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
  351. CGFloat width = 0, height = 0;
  352. if (imageSourceRef) {
  353. CFDictionaryRef imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSourceRef, 0, NULL);
  354. //以下是对手机32位、64位的处理
  355. if (imageProperties != NULL) {
  356. CFNumberRef widthNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelWidth);
  357. #if defined(__LP64__) && __LP64__
  358. if (widthNumberRef != NULL) {
  359. CFNumberGetValue(widthNumberRef, kCFNumberFloat64Type, &width);
  360. }
  361. CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
  362. if (heightNumberRef != NULL) {
  363. CFNumberGetValue(heightNumberRef, kCFNumberFloat64Type, &height);
  364. }
  365. #else
  366. if (widthNumberRef != NULL) {
  367. CFNumberGetValue(widthNumberRef, kCFNumberFloat32Type, &width);
  368. }
  369. CFNumberRef heightNumberRef = CFDictionaryGetValue(imageProperties, kCGImagePropertyPixelHeight);
  370. if (heightNumberRef != NULL) {
  371. CFNumberGetValue(heightNumberRef, kCFNumberFloat32Type, &height);
  372. }
  373. #endif
  374. CFRelease(imageProperties);
  375. }
  376. CFRelease(imageSourceRef);
  377. }
  378. return CGSizeMake(width, height);
  379. }
  380. @end