我正在尝试对 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 的。
它最终看起来像这样:
但我想与这张图片相匹配:
最佳答案
考虑 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/