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

时间:2017-06-18 21:17:49

标签: ios iphone swift uiimagepickercontroller

我目前正在创建相机应用程序,但是当有人拍照并进入裁剪屏幕时,它实际上并不适合该特定尺寸。如何更改代码以使其仅吐出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.
    }
    */

}

1 个答案:

答案 0 :(得分:1)

我不知道如何将作物限制为特定的宽高比。相反,我建议您创建自己的裁剪图像的方法。

我在Github上有一个名为CropImg的应用程序。它不会将作物限制在正方形,但这很容易添加。