[IOS]当图片太大时,重新调整图片大小

网友投稿 1313 2022-09-02

[IOS]当图片太大时,重新调整图片大小

[IOS]当图片太大时,重新调整图片大小

图片分辨率太高,有时候直接放在button里面会导致变形,需要重新缩放图片。

直接上代码:

.h:

@import UIKit;@interface UIImage (PGResizeImage)- (UIImage *)convertToGreyscale;- (UIImage *)resizeImageToDimension:(CGSize)expectedSize;- (UIImage *)resizeImageWithScaledToSize:(CGSize)newSize;- (UIImage *)setResizeImageWithSize:(NSInteger)size;- (UIImage *)waterMarkWithImage:(UIImage *)waterMark;@end

.m:

#import "UIImage+PGResizeImage.h"//#import "PGLogger.h"@implementation UIImage (PGResizeImage)- (UIImage *)convertToGreyscale { int kRed = 1; int kGreen = 2; int kBlue = 4; int colors = kGreen | kBlue | kRed; int m_width = self.size.width; int m_height = self.size.height; uint32_t *rgbImage = (uint32_t *) malloc(m_width * m_height * sizeof(uint32_t)); CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); CGContextRef context = CGBitmapContextCreate(rgbImage, m_width, m_height, 8, m_width * 4, colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGContextSetInterpolationQuality(context, kCGInterpolationHigh); CGContextSetShouldAntialias(context, NO); CGContextDrawImage(context, CGRectMake(0, 0, m_width, m_height), [self CGImage]); CGContextRelease(context); CGColorSpaceRelease(colorSpace); uint8_t *m_imageData = (uint8_t *) malloc(m_width * m_height); for(int y = 0; y < m_height; y++) { for(int x = 0; x < m_width; x++) { uint32_t rgbPixel = rgbImage[y * m_width + x]; uint32_t sum = 0,count = 0; if (colors & kRed) { sum += (rgbPixel >> 24)&255; count++; } if (colors & kGreen) { sum += (rgbPixel >> 16)&255; count++; } if (colors & kBlue) { sum += (rgbPixel >> 8)&255; count++; } m_imageData[y*m_width+x]=sum/count; } } free(rgbImage); uint8_t *result = (uint8_t *) calloc(m_width * m_height *sizeof(uint32_t), 1); for(int i = 0; i < m_height * m_width; i++) { result[i * 4] = 0; int val = m_imageData[i]; result[i * 4 + 1]=val; result[i * 4 + 2]=val; result[i * 4 + 3]=val; } colorSpace = CGColorSpaceCreateDeviceRGB(); context = CGBitmapContextCreate(result, m_width, m_height, 8, m_width * sizeof(uint32_t), colorSpace, kCGBitmapByteOrder32Little | kCGImageAlphaNoneSkipLast); CGImageRef imageRef = CGBitmapContextCreateImage(context); CGContextRelease(context); CGColorSpaceRelease(colorSpace); UIImage *resultUIImage = [UIImage imageWithCGImage:imageRef]; CGImageRelease(imageRef); free(m_imageData); [NSData dataWithBytesNoCopy:result length:m_width * m_height]; return resultUIImage;}- (UIImage *)resizeImageToDimension:(CGSize)expectedSize {// DDLogInfo(@"%@ originalSize: %@, expectedSize: %@", NSStringFromSelector(_cmd), NSStringFromCGSize(self.size), NSStringFromCGSize(expectedSize)); CGFloat totalExpectedSize = expectedSize.height * expectedSize.width; if (totalExpectedSize < self.size.height * self.size.width) { CGFloat ratio; if (self.size.width < self.size.height) { expectedSize = CGSizeMake(expectedSize.height, expectedSize.width); ratio = self.size.width / expectedSize.width; } else if (self.size.height < self.size.width) { ratio = self.size.height / expectedSize.height; } else { ratio = self.size.height / MIN(expectedSize.height, expectedSize.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 *)resizeImageWithScaledToSize:(CGSize)newSize { UIGraphicsBeginImageContextWithOptions(newSize, NO, 0); [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage;}- (UIImage *)setResizeImageWithSize:(NSInteger)size { CGSize imageSize = self.size; if (imageSize.width > size || imageSize.height > size) { CGFloat scaleRatio = MIN( size / imageSize.width , size / imageSize.height); CGSize scaledSize = {imageSize.width * scaleRatio, imageSize.height * scaleRatio};// DDLogDebug(@"Size: %@", NSStringFromCGSize(scaledSize)); UIGraphicsBeginImageContextWithOptions(scaledSize, NO, 0); [self drawInRect:CGRectMake(0, 0, scaledSize.width, scaledSize.height)]; UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return newImage; } return self;}- (UIImage *)waterMarkWithImage:(UIImage *)waterMark { waterMark = [waterMark resizeImageWithScaledToSize:CGSizeMake(800, 200)]; UIImage *watermarkedImage = nil; UIGraphicsBeginImageContext(self.size); [self drawAtPoint: CGPointZero]; [waterMark drawInRect:CGRectMake(self.size.width / 2 - waterMark.size.width / 2, self.size.height / 2 - waterMark.size.height / 2, waterMark.size.width, waterMark.size.height)]; watermarkedImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return watermarkedImage;}

使用:

self.checkedNormalImage = [[UIImage imageNamed:@"btn_untick"] resizeImageToDimension:CGSizeMake(16, 16)];

这样可以适配大小

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:程序员提高效率的必备工具(高效的工具可以提升工作效率)
下一篇:[IOS]iphoneX简单适配
相关文章

 发表评论

暂时没有评论,来抢沙发吧~