[英]How to upload images to azure without losing quality with image data swift
[英]Make UIImage a Circle without Losing quality? (swift)
一旦应用roundImage,图像就会变得模糊: 将UIImage制作为圆形
extension UIImage
{
func roundImage() -> UIImage
{
let newImage = self.copy() as! UIImage
let cornerRadius = self.size.height/2
UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
let bounds = CGRect(origin: CGPointZero, size: self.size)
UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
newImage.drawInRect(bounds)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
return finalImage
}
}
试试这个
let image = UIImageView(frame:CGRectMake(0,0,100,100))
我建议您可以使用AlamofireImage( https://github.com/Alamofire/AlamofireImage )
制作圆形图像或圆形图像非常容易而又不损失质量。
像这样:
let image = UIImage(named: "unicorn")!
let radius: CGFloat = 20.0
let roundedImage = image.af_imageWithRoundedCornerRadius(radius)
let circularImage = image.af_imageRoundedIntoCircle()
瞧!
为什么使用bezierpath
? 只需为uiimageview
设置cornerradius
uiimageview
。
如果图像大于imageview
则必须将image
调整为您的imageview
大小,然后为该uiimageview
设置cornerradius
uiimageview
。
它会工作。 为我工作
替换以下行
UIGraphicsBeginImageContextWithOptions(self.size, false, 1.0)
与
UIGraphicsBeginImageContextWithOptions(self.size, view.opaque , 0.0)
您的问题是您正在使用标度1,这是最低的“质量”。 将比例尺设置为0将使用设备比例尺,它仅按原样使用图像。
注意:类内部的返回该类新实例的函数可以实现为类函数。 这使函数很清楚。 它不操纵现有图像。 它返回一个新的。
由于您在谈论圆圈,因此我也更正了您的代码,现在它可以对任何图像进行圆圈绘制并裁剪。 您可能需要将此居中。
extension UIImage {
class func roundImage(image : UIImage) -> UIImage? {
// copy
guard let newImage = image.copy() as? UIImage else {
return nil
}
// start context
UIGraphicsBeginImageContextWithOptions(newImage.size, false, 0.0)
// bounds
let cornerRadius = newImage.size.height / 2
let minDim = min(newImage.size.height, newImage.size.width)
let bounds = CGRect(origin: CGPointZero, size: CGSize(width: minDim, height: minDim))
UIBezierPath(roundedRect: bounds, cornerRadius: cornerRadius).addClip()
// new image
newImage.drawInRect(bounds)
let finalImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
// crop
let maybeCrop = UIImage.crop(finalImage, cropRect: bounds)
return maybeCrop
}
class func crop(image: UIImage, cropRect : CGRect) -> UIImage? {
guard let imgRef = CGImageCreateWithImageInRect(image.CGImage, cropRect) else {
return nil
}
return UIImage(CGImage: imgRef)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.