我正在使用 AlamofireImage 下载并设置我的 UIImage:
backdrop.af_setImageWithURL(downloadURL)
图像比我将要显示的要大得多,所以我遇到了锯齿问题。如何正确调整此图像的大小?
结果:
我正在使用 AlamofireImage 下载并设置我的 UIImage:
backdrop.af_setImageWithURL(downloadURL)
图像比我将要显示的要大得多,所以我遇到了锯齿问题。如何正确调整此图像的大小?
结果:
一旦你有一个有效的,你可以调整任何大小的图像UIImage
:
func resizedImageWith(image: UIImage, targetSize: CGSize) -> UIImage {
let imageSize = image.size
let newWidth = targetSize.width / image.size.width
let newHeight = targetSize.height / image.size.height
var newSize: CGSize
if(newWidth > newHeight) {
newSize = CGSizeMake(imageSize.width * newHeight, imageSize.height * newHeight)
} else {
newSize = CGSizeMake(imageSize.width * newWidth, imageSize.height * newWidth)
}
let rect = CGRectMake(0, 0, newSize.width, newSize.height)
UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0)
image.drawInRect(rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage
}
对于您有任何其他查询/修改,请参阅此NShipster
这是@Santosh 在 ObjC 中的回答:
- (UIImage*) scaleImage:(UIImage*)image toSize:(CGSize)newSize {
CGSize imageSize = image.size;
CGFloat newWidth = newSize.width / image.size.width;
CGFloat newHeight = newSize.height / image.size.height;
CGSize newImgSize;
if(newWidth > newHeight) {
newImgSize = CGSizeMake(imageSize.width * newHeight, imageSize.height * newHeight);
} else {
newImgSize = CGSizeMake(imageSize.width * newWidth, imageSize.height * newWidth);
}
CGRect rect = CGRectMake(0, 0, newImgSize.width, newImgSize.height);
UIGraphicsBeginImageContextWithOptions(newImgSize, false, 0.0);
[image drawInRect:rect];;
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}