如何在 Swift 3 App 中裁剪 iPhone 图片

How to Crop iPhone Image in Swift 3 App

我目前正在创建一个相机应用程序,但是当有人拍照并进入裁剪屏幕时,它实际上并不适合特定尺寸。我怎样才能更改我的代码以使其仅吐出 800x800 图像?

import UIKit
import Firebase
class CameraControllerViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var postBtn: UIButton!
    @IBOutlet weak var pickedimage: UIImageView!
    @IBOutlet weak var libBtn: UIButton!
    @IBOutlet weak var camBtn: UIButton!
    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view.
    }
    @IBAction func camerabuttonaction(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.camera;
            imagePicker.allowsEditing = false
            self.present(imagePicker, animated:true, completion: nil)
        }
    }

    @IBAction func photolibraryaction(_ sender: Any) {
        if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {
            let imagePicker = UIImagePickerController()
            imagePicker.delegate = self
            imagePicker.sourceType = UIImagePickerControllerSourceType.photoLibrary;
            imagePicker.allowsEditing = true
            self.present(imagePicker, animated: true, completion: nil)

        }
    }

    @IBAction func saveaction(_ sender: Any) {
        let imageData = UIImageJPEGRepresentation(pickedimage.image!, 0.6)
        let compressedJPEGImage = UIImage(data: imageData!)
        UIImageWriteToSavedPhotosAlbum(compressedJPEGImage!, nil, nil, nil)
        saveNotice()


    }

    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingImage image: UIImage!, editingInfo: [NSObject : AnyObject]! ) {
            pickedimage.image = image
            camBtn.isHidden = true
            libBtn.isHidden = true
            postBtn.isHidden = false
            self.dismiss(animated: true, completion: nil);
    }

    func saveNotice() {
        AppDelegate.instance().showActivityIndicator()
        //let alertController = UIAlertController(title: "Image Saved", message: "Your picture was saved.", preferredStyle: .alert)

        //let defaultAction =  UIAlertAction(title: "OK", style: .default, handler: nil)
        //alertController.addAction(defaultAction)
        //present(alertController, animated: true, completion: nil)
        let uid = Auth.auth().currentUser!.uid
        let ref = Database.database().reference()
        let storage = Storage.storage().reference(forURL: "gs://new-glaance.appspot.com")
        let key = ref.child("posts").childByAutoId().key
        let imageRef = storage.child("posts").child(uid).child("\(key).jpg")


        let data = UIImageJPEGRepresentation(self.pickedimage.image!, 0.6)

        let uploadTask = imageRef.putData(data!, metadata: nil) { (metadata,error) in
            if error != nil {
                print(error!.localizedDescription)
                AppDelegate.instance().dismissActivityIndicator()
                return
            }

            imageRef.downloadURL(completion: { (url, error) in
                if let url = url {
                    let feed = ["userID": uid,
                                "pathToImage": url.absoluteString,
                                "likes":0,
                                "author": Auth.auth().currentUser!.displayName!,
                                "postID": key] as [String: Any]

                    let postFeed =  ["\(key)":feed]

                    ref.child("posts").updateChildValues(postFeed)
                    AppDelegate.instance().dismissActivityIndicator()

                }
            })
        }
        uploadTask.resume()
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

我不知道有什么方法可以将裁剪限制为特定的纵横比。相反,我建议创建您自己的裁剪图像的方法。

我在 Github 上有一个名为 CropImg 的应用程序就是这样做的。它不会将裁剪限制为正方形,但添加起来很容易。