1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

PINRemoteImageで取得する画像を角丸&影をつける

Last updated at Posted at 2018-10-02

画像を角丸にして影をつける

PINRemoteImageで取得した画像を角丸にして影をつけて表示する処理を実装する機会があったのでその方法を記録します。
角丸にして影をつける処理では、Viewを2つ重ねるサンプルをよく見かけたのですが、Imageで角丸にするやりかたにしました。

(前提) PINRemoteImageをPod等で導入済

  1. UIImageViewを配置

storyboardで配置し、outletをつけておきます
スクリーンショット 2018-09-28 19.25.35.png

  1. UIImageViewに画像URLを設定し、画像を角丸に。UIImageViewに影を設定
@IBOutlet var imageView: UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        imageView.pin_updateWithProgress = true
        let imageSize = imageView.frame.size
        imageView.pin_setImage(from: URL(string: "https://upload.wikimedia.org/wikipedia/commons/9/9a/Gull_portrait_ca_usa.jpg"), processorKey: "rounded") { (result, unsafePointer) -> UIImage? in
            guard let image = result.image else {
                return nil
            }
            // 角丸にする処理
            UIGraphicsBeginImageContext(imageSize)
            UIGraphicsBeginImageContextWithOptions(imageSize, false, 0.0)
            let rect = CGRect(origin: .zero, size: imageSize)
            UIBezierPath(roundedRect: rect, cornerRadius: 5).addClip()
            image.draw(in: rect)
            let clippedImage = UIGraphicsGetImageFromCurrentImageContext()

            UIGraphicsEndImageContext()
            return clippedImage
        }

        // 影をつける処理
        imageView.layer.masksToBounds = false
        imageView.layer.shadowColor = UIColor.black.cgColor
        imageView.layer.shadowOffset = CGSize(width: 2, height: 2)
        imageView.layer.shadowRadius = 5
        imageView.layer.shadowOpacity = 0.3
    }
スクリーンショット 2018-10-01 10.46.45.png
  1. ちなみに

PINRemoteImageを利用しないでローカル画像を利用する場合は以下のようなかんじ

if let image = UIImage(named: "kamome.jpg") {
            // 角丸にする処理
            UIGraphicsBeginImageContext(imageView.frame.size)
            UIGraphicsBeginImageContextWithOptions(imageView.frame.size, false, 0.0)
            let rect = CGRect(origin: .zero, size: imageView.frame.size)
            UIBezierPath(roundedRect: rect, cornerRadius: 5).addClip()
            image.draw(in: rect)
            let clippedImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            imageView.image = clippedImage
            
            // 影をつける処理
            imageView.layer.masksToBounds = false
            imageView.layer.shadowColor = UIColor.black.cgColor
            imageView.layer.shadowOffset = CGSize(width: 2, height: 2)
            imageView.layer.shadowRadius = 5
            imageView.layer.shadowOpacity = 0.3
        }
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?