gpt4 book ai didi

ios - UIImage 方面填充

转载 作者:可可西里 更新时间:2023-11-01 02:00:00 25 4
gpt4 key购买 nike

我正在尝试对 UIImage 执行方面填充(使用可重用扩展),但我只做到了这一点:

extension UIImage {
func resizeToCircleImage(targetSize: CGSize, contentMode: UIViewContentMode) -> UIImage {
UIGraphicsBeginImageContextWithOptions(targetSize, true, 0.0)
let rect = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
self.draw(in: rect)
let newImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return newImage!
}
}

因此,这是针对 MKAnnotationView 的。

最终看起来像这样:

enter image description here

但我想纵横比适合这张图片:

https://en.wikipedia.org/wiki/Panorama#/media/File:Panorama_of_the_courtyard_of_the_Great_Mosque_of_Kairouan.jpg

最佳答案

考虑一个类别的 UIImage,

- (UIImage *)aspectFillToSize:(CGSize)size
{
CGFloat imgAspect = self.size.width / self.size.height;
CGFloat sizeAspect = size.width/size.height;

CGSize scaledSize;

if (sizeAspect > imgAspect) { // increase width, crop height
scaledSize = CGSizeMake(size.width, size.width / imgAspect);
} else { // increase height, crop width
scaledSize = CGSizeMake(size.height * imgAspect, size.height);
}
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextClipToRect(context, CGRectMake(0, 0, size.width, size.height));
[self drawInRect:CGRectMake(0.0f, 0.0f, scaledSize.width, scaledSize.height)];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}

关于ios - UIImage 方面填充,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47370110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com