如何圈选图像

时间:2016-02-26 16:56:51

标签: ios swift uiimageview uiimage

您好我在我的应用中使用 SDWebImage 。这是我用圆圈制作图片的代码

extension UIImage {
var circle: UIImage? {
        let square = CGSize(width: min(size.width, size.height), height: min(size.width, size.height))
        let imageView = UIImageView(frame: CGRect(origin: CGPoint(x: 0, y: 0), size: square))
        imageView.contentMode = .ScaleAspectFill
        imageView.image = self
        imageView.layer.cornerRadius = square.width/2
        imageView.layer.masksToBounds = true
        UIGraphicsBeginImageContext(imageView.bounds.size)
        guard let context = UIGraphicsGetCurrentContext() else { return nil }
        imageView.layer.renderInContext(context)
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
}

here

复制

我曾经像这样在cicle中做图像

let profilePicture = UIImage(data: NSData(contentsOfURL: NSURL(string:"http://i.stack.imgur.com/Xs4RX.jpg")!)!)!

profilePicture.circle

但是现在我正在使用SDWebImage它无法正常工作

 cell.profileImageView.sd_setImageWithURL(UIImage().absoluteURL(profileImageUrl), placeholderImage: UIImage.init(named: "default-profile-icon")?.circle!)

请告诉我如何才能使此扩展程序适用于SDWebImage

1 个答案:

答案 0 :(得分:3)

您可以使用SDWebImageManager下载图片或从缓存中获取图片并在完成模块中应用圆圈,如下所示:

SDWebImageManager.sharedManager().downloadWithURL(NSURL(string:"img"), options: [], progress: nil) { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool) -> Void in
    if (image != nil){
        let circleImage = image.circle
        cell.profileImageView.image = circleImage
    }
}

或者您可以使用将完成块作为参数的sd_setImageWithURL方法的版本

let completionBlock: SDWebImageCompletionBlock! = {(image: UIImage!, error: NSError!, cacheType: SDImageCacheType!, imageURL: NSURL!) -> Void in
    if (image != nil){
        let circleImage = image.circle
        cell.profileImageView.image = circleImage
    }
}
cell.profileImageView.sd_setImageWithURL(UIImage().absoluteURL(profileImageUrl), placeholderImage: UIImage.init(named: "default-profile-icon")?.circle!, completed: completionBlock)