iOS 圖片處理方法(按比例縮放,指定寬度按比例縮放)
阿新 • • 發佈:2019-01-11
今天遇見的處理圖片的問題,一張圖片上下兩個部分都有一個空白區域,就中間是圖片。要求是不能讓他顯示上下 有空白問間距。這是測試提出來的問題,但是圖片本身就是這個毛病。無奈,哥哥改。誰有好的方法推薦推薦推薦 啊!!!
兩個方法如下
建議讓這兩個放在UIImage的延展裡,方便以後使用。
1:按比例縮放
在這裡你傳入的cgsize就是你要放大的區域,或者就是說設定放大的區域部分
//按比例縮放,size 是你要把圖顯示到 多大區域 + (UIImage *) imageCompressFitSizeScale:(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; }
2:按比例縮放
這個程式碼很有意思
//指定寬度按比例縮放 -(UIImage *) imageCompressForWidthScale:(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; }
呼叫
UIImage *headImage = [UIImage imageWithContentsOfFile:imagePath];
UIImage * newHeadImage = [UIImage imageCompressForSize:headImage targetSize:CGSizeMake(200, 140)];
if (newHeadImage==nil) {
newHeadImage = [UIImage imageNamed:@"headIcon"];
}
self.headerImageView.image = newHeadImage;