Help us understand the problem. What is going on with this article?

【iOS】画像トリミングをサクッとライブラリで実現

はじめに

こんにちは、@kaoryuuu です。
AdventCalendar2018 LIFULLその2の8日目の投稿になります。
この記事は元々ぼっち向けインスタライクな日記アプリを作ろうと思った際に実装した内容になります。

概要

画像の加工を行う際にトリミングや画像の回転などは欠かせない機能の一つです。
今回はこちらのライブラリを使ってお手軽に実装していきます。

https://github.com/TimOliver/TOCropViewController

作るもの

CropViewController


1.Pod

pod 'CropViewController'

2.import✋

UIViewController.swift
import CropViewController

3.Call☎️

画像をセットしてpresentしてあげるだけ!あとはDelegateに任せましょう

UIViewController.swift
let cropViewController = CropViewController(image: UIImage)
cropViewController.delegate = self
present(cropViewController, animated: true, completion: nil)

4.Delegate🤖

Delegateがいくつか用意されているのでお好みで使いたいものを使いましょう。
↓のは最低限実装に使いそうなもの

UIViewController.swift
extension UIViewController: CropViewControllerDelegate {

    func cropViewController(_ cropViewController: CropViewController, didCropToImage image: UIImage, withRect cropRect: CGRect, angle: Int) {
        //加工した画像が取得できる
    }

    func cropViewController(_ cropViewController: CropViewController, didFinishCancelled cancelled: Bool) {
        // キャンセル時
        cropViewController.dismiss(animated: true, completion: nil)
    }
}

完成

demo

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away