【问题标题】:How to Crop iPhone Image in Swift 3 App如何在 Swift 3 应用程序中裁剪 iPhone 图像
【发布时间】:2017-11-21 00:34:40
【问题描述】:

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

}

【问题讨论】:

  • 您还没有发布任何裁剪图像的代码,您尝试设置图像的大小。您希望用户将图像裁剪为正方形,然后将其缩放到 800x800 像素,还是让用户选择图像的 800x800 像素区域?
  • 让用户选择比例为 1x1 的图像部分,输出为 800x800
  • 那么您是否编写了任何代码让用户选择源图像的正方形区域?如果用户选择的区域小于 800x800 像素,你想做什么?在这种情况下你会扩大规模吗?
  • 我唯一拥有的就是'imagePicker.allowsEditing = true'。是的。

标签: ios iphone swift uiimagepickercontroller


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2017-04-12
    • 2011-12-18
    • 2020-08-14
    • 2011-09-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    相关资源
    最近更新 更多