我一直在寻找许多堆栈溢出帖子,但没有人能够给我我想要的解决方案。到目前为止,我已经能够使用AlamoFire获取图像并将其转换为圆形。但是,遗憾的是,alamo fire并未提供为UIImage添加边框的选项。我想知道是否有人解决了我的问题。这是我将图像变成圆形的代码:
if let downloadedImage = UIImage(data: data!) {
let markerImage = downloadedImage
let markerImageSize = CGSize(width: 50, height: 50)
let markerImageFilter = AspectScaledToFillSizeCircleFilter(size: markerImageSize)
let finalMarkerImage = markerImageFilter.filter(markerImage)
marker.icon = finalMarkerImage
}
正如你所看到的,我能够获得一个圆圈而不是一个有边框的圆圈。到目前为止,我已经尝试了许多堆栈溢出后解决方案来尝试使用我的AlamoFire解决方案。以下是一些帖子:Making a UIImage to a circle form
Cut a UIImage into a circle Swift(iOS)
这是我目前拥有的:
这就是我想要的:
任何帮助将非常感激。谢谢!
我建议你应该将所需的外观应用到包含你的UIImage的UIImageView,如下所示:
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.layer.masksToBounds = true
imageView.layer.borderWidth = 2
imageView.layer.borderColor = UIColor.brown.cgColor
更新:
由于您正在使用Google Maps(GMSMarker
),您应该以编程方式创建UIImageView(将上面的代码片段应用于它)并将其作为iconView
添加到您的标记中,如下所示:
marker.iconView = imageView
所以,它应该类似于:
// of course the values of the width/height (size) is up to you
let imageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 100, height: 100))
imageView.layer.cornerRadius = imageView.frame.size.width / 2
imageView.layer.masksToBounds = true
imageView.layer.borderWidth = 2
imageView.layer.borderColor = UIColor.white.cgColor
// set your image
imageView.image = ...
marker.iconView = imageView
这应该创建带有白色边框的圆形图像......
func round(image: UIImage) -> UIImage {
let imageWidth = image.size.width
let imageHeight = image.size.height
let diameter = min(imageWidth, imageHeight)
let isLandscape = imageWidth > imageHeight
let xOffset = isLandscape ? (imageWidth - diameter) / 2 : 0
let yOffset = isLandscape ? 0 : (imageHeight - diameter) / 2
let imageSize = CGSize(width: diameter, height: diameter)
return UIGraphicsImageRenderer(size: imageSize).image { _ in
let ovalPath = UIBezierPath(ovalIn: CGRect(origin: .zero, size: imageSize))
ovalPath.addClip()
image.draw(at: CGPoint(x: -xOffset, y: -yOffset))
UIColor.white.setStroke()
ovalPath.lineWidth = diameter / 50
ovalPath.stroke()
}
}
然后
let roundImage = round(image: downloadedImage)
对于那些与@ashley回答的obj-c版本挣扎的人。同样的逻辑
+ (UIImage *)drawBorderToImage:(UIImage *)image withColor:(UIColor *)color andThickness:(CGFloat)thickness {
CGFloat diameter = MIN(image.size.width, image.size.height);
BOOL isLandscape = image.size.width > image.size.height;
CGFloat xOffset = isLandscape ? (image.size.width - diameter) / 2 : 0;
CGFloat yOffset = isLandscape ? 0 : (image.size.height - diameter) / 2;
CGSize imageSize = CGSizeMake(diameter, diameter);
UIGraphicsBeginImageContext(image.size);
UIBezierPath *ovalPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)];
[ovalPath addClip];
[image drawAtPoint:CGPointMake(-xOffset, -yOffset)];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetStrokeColorWithColor(context, color.CGColor);
ovalPath.lineWidth = thickness;
[ovalPath stroke];
UIImage *borderedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return borderedImage;
}