Tocropviewcontroller: ๋ฉ”๋ชจ๋ฆฌ ๋ˆ„์ˆ˜ ๊ฐ€๋Šฅ์„ฑ์ด ์žˆ์Šต๋‹ˆ๊นŒ?

์— ๋งŒ๋“  2018๋…„ 03์›” 08์ผ  ยท  4์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: TimOliver/TOCropViewController

์ €๋Š” ์‹ ์†ํ•˜๊ฒŒ 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 ๋ฅผ ํ˜ธ์ถœํ•ด๋„ ํ˜ธ์ถœ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์ข‹์•„ .. ๋‚˜๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ทธ๊ฒƒ์„ ๊ณ ์ณค๋‹ค.
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)
            }
        }
    }

๋ชจ๋“  4 ๋Œ“๊ธ€

์•ˆ๋…•ํ•˜์„ธ์š” @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์— ๋ณ‘ํ•ฉ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค!

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰