์ ๋ ์ ์ํ๊ฒ CropViewController๋ฅผ ์ฌ์ฉํ๊ณ ์์ผ๋ฉฐ ์ด์ ๋ฐฉ๊ธ ์ค์นํ์ฌ ์ ํฌ๋๊ฐ ์ต์ ์ํ์ ๋๋ค. ์ ๋ง ์ข์, ์ํ์ด, ํ์ง๋ง ๋ฉ๋ชจ๋ฆฌ ๋์๊ฐ ๋ฐ์ํ์ ๊ฐ๋ฅ์ฑ์ด ์๋ค๊ณ ์๊ฐํฉ๋๋ค.
๋ฐฉ๊ธ ์ UIViewController๋ฅผ ๋ง๋ค๊ณ ๋ชจ๋ฌ์ ํตํด ํ์ํฉ๋๋ค. ์ด ์ปจํธ๋กค๋ฌ์ UIImageView๊ฐ ์๊ณ ํญ ์ธ์๊ธฐ๊ฐ ์์ต๋๋ค. UIImagePickerController๋ฅผ ์ฌ์ฉํ๊ณ ๋ฏธ๋์ด ์ ํ์ด ์๋ฃ๋ ๋ CropViewController ๊ฐ์ฒด๋ฅผ ํ์ํฉ๋๋ค.
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
let cropVC = CropViewController(croppingStyle: .circular, image: selectedImage)
cropVC.delegate = self
picker.present(cropVC, animated: false, completion: nil)
}
}
์ด UIViewController๋ฅผ ์๋ฅด๊ณ ๋ซ์ ํ deinit
ํธ์ถ๋์ง ์์์ต๋๋ค. imagePickerController
๋๋ฆฌ์ ๋ฉ์๋๋ฅผ ๋ค์๊ณผ ๊ฐ์ด ๋ณ๊ฒฝํ๋ ๊ฒฝ์ฐ
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
self.imageView.image = selectedImage
}
}
๋ด๊ฐ ํด๊ณ ํ๋ฉด deinit
์ ํ๋ฅผ ๊ฒ๋๋ค. ์ฌ๊ธฐ์๋ ํน๋ณํ ํญ๋ชฉ์ ์ค์ ํ์ง ์๊ณ UIViewController, UIImageView ๋ฐ UIImagePickerController๋ง ์ค์ ํฉ๋๋ค. ๋ด๊ฐ ์ค์ ์ ์๋ชปํ์ต๋๊น ์๋๋ฉด ๋ฉ๋ชจ๋ฆฌ ๋์์
๋๊น?
CropViewController.swift
ํด๋์ค์์ deinit
๋ฅผ ํธ์ถํด๋ ํธ์ถ๋์ง ์์ต๋๋ค.
์๋ ํ์ธ์ @kcankaynak์ ๋๋ค!
์ด ์ค. ๋ค๊ฐ ์ณ์. ํ์คํ ๋ฉ๋ชจ๋ฆฌ ๋์ ๊ฐ์ ๋์๊ฐ ๋ฉ๋๋ค. ๊ธฐํ๊ฐ๋๋ค๋ฉด ์ํ ์ฑ์์ ์๋ ํด๋ด์ผ ๊ฒ ์ด์. ์๋ ค ์ค์ ๊ณ ๋ง์!
๋๋ ๊ฐ์ ๋ฌธ์ ๊ฐ ์์ต๋๋ค. ๋ด UIViewController(๊ทํ์ ์๋ฅด๊ธฐ VC๋ฅผ ํ์ํ๋ ๊ฒ)๊ฐ ํด์ ๋์ง ์๋๋ก ํ๋ ์ ์ง ์ฃผ๊ธฐ๊ฐ ์์ผ๋ฉฐ @kcankaynak์ด ์ธ๊ธํ ๋๋ก deinit ๋ฉ์๋๋ฅผ ํธ์ถํ์ง ์์ต๋๋ค. ๊ทํ์ ๋๋ฆฌ์ธ ์์ฑ์ด
public var delegate: CropViewControllerDelegate? {
didSet { self.setUpDelegateHandlers() }
}
๊ทธ๋์ ๋๋ ์๊ฐํ๋ค.. ์๋ง๋
๊ณต๊ฐ ์ฝํ var ๋๋ฆฌ์: CropViewControllerDelegate? {
didSet { self.setUpDelegateHandlers() }
}
๊ทธ๊ฒ์ ๊ณ ์น ๊ฒ์ ๋๋ค ...ํ์ง๋ง ๊ทธ๊ฒ์ํ์ง ์์์ต๋๋ค :(
์ข์ .. ๋๋ ๋ค์๊ณผ ๊ฐ์ด ๊ทธ๊ฒ์ ๊ณ ์ณค๋ค.
1- ๋๋ฆฌ์ธ์ ํญ์ ์ฝํด์ผ ํ๋ฏ๋ก ๋๋ฆฌ์ธ์ ์ฝํ๊ฒ ๋ง๋ค์์ต๋๋ค.
2- ์ฒ๋ฆฌ๊ธฐ ๋ด๋ถ์์ ์์ฒด ํธ์ถ์ [์ฝํ ์์ฒด]๋ก ๋ง๋ค์์ต๋๋ค.
CropViewController.swift ์์ onDid... ์ฝ๋ฐฑ์ ํ์ธ
fileprivate func setUpDelegateHandlers() {
guard let delegate = self.delegate else {
onDidCropToRect = nil
onDidCropImageToRect = nil
onDidCropToCircleImage = nil
onDidFinishCancelled = nil
return
}
if delegate.responds(to: #selector(CropViewControllerDelegate.cropViewController(_:didCropImageToRect:angle:))) {
self.onDidCropImageToRect = {[weak self] rect, angle in
delegate.cropViewController!((self)!, didCropImageToRect: rect, angle: angle)
}
}
if delegate.responds(to: #selector(CropViewControllerDelegate.cropViewController(_:didCropToImage:withRect:angle:))) {
self.onDidCropToRect = {[weak self] image, rect, angle in
delegate.cropViewController!((self)!, didCropToImage: image, withRect: rect, angle: angle)
}
}
if delegate.responds(to: #selector(CropViewControllerDelegate.cropViewController(_:didCropToCircularImage:withRect:angle:))) {
self.onDidCropToCircleImage = {[weak self] image, rect, angle in
delegate.cropViewController!((self)!, didCropToCircularImage: image, withRect: rect, angle: angle)
}
}
if delegate.responds(to: #selector(CropViewControllerDelegate.cropViewController(_:didFinishCancelled:))) {
self.onDidFinishCancelled = { [weak self] finished in
delegate.cropViewController!((self)!, didFinishCancelled: finished)
}
}
}
์ด๊ฒ์ ๋ค๋ฅธ PR์ ๋ณํฉ๋์์ต๋๋ค. ๊ฐ์ฌํฉ๋๋ค!
๊ฐ์ฅ ์ ์ฉํ ๋๊ธ
์ข์ .. ๋๋ ๋ค์๊ณผ ๊ฐ์ด ๊ทธ๊ฒ์ ๊ณ ์ณค๋ค.
1- ๋๋ฆฌ์ธ์ ํญ์ ์ฝํด์ผ ํ๋ฏ๋ก ๋๋ฆฌ์ธ์ ์ฝํ๊ฒ ๋ง๋ค์์ต๋๋ค.
2- ์ฒ๋ฆฌ๊ธฐ ๋ด๋ถ์์ ์์ฒด ํธ์ถ์ [์ฝํ ์์ฒด]๋ก ๋ง๋ค์์ต๋๋ค.
CropViewController.swift ์์ onDid... ์ฝ๋ฐฑ์ ํ์ธ