Ios 图像方面填充
我试图在UIImage上执行方面填充(使用可重用扩展),但我只做到了这一点:Ios 图像方面填充,ios,swift,uiimage,core-graphics,Ios,Swift,Uiimage,Core Graphics,我试图在UIImage上执行方面填充(使用可重用扩展),但我只做到了这一点: extension UIImage { func resizeToCircleImage(targetSize: CGSize, contentMode: UIViewContentMode) -> UIImage { UIGraphicsBeginImageContextWithOptions(targetSize, true, 0.0) let rect = CGRec
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;
}
你能解释一下预期结果和当前结果吗?屏幕截图可能有助于您说您正试图在uiimage上填充方面,但您希望方面适合。嗯。怎么回事?!?!我想这两个都可以。如果我知道一种方法,我应该能够适应另一种方法。希望我没那么笨。