如何在IOS应用中显示图片?

时间:2018-07-14 13:25:12

标签: ios iphone swift xcode image

我是构建IOS应用程序的初学者,我想知道在应用程序上显示用户图片的良好做法。我在google上搜索了一些练习或文档,但我什么都没找到...我的问号是:

  

当我们想要“人像照片”或“自拍照”时,常用的比例是什么?

对于我的应用程序,我需要显示固定比例的图像,我想授权用户裁剪图像,所以您知道一个可以帮助我做到这一点的图书馆吗?

感谢您的答复

1 个答案:

答案 0 :(得分:1)

这就是我正在使用的:

displayPhotoImageView是加载了uiimageView照片的

var newpic:Bool?

  func photoAction(){

        let myalert=UIAlertController(title:"",message:"choose photo  ",preferredStyle: .actionSheet)

        let camerAction=UIAlertAction(title:"camera",style:.default){(action) in

            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera) {

                let imagepicker = UIImagePickerController()
                imagepicker.delegate = self
                imagepicker.sourceType = UIImagePickerControllerSourceType.camera
                imagepicker.mediaTypes = [kUTTypeImage as! String]
                imagepicker.allowsEditing = false

                self.present(imagepicker, animated: true, completion: nil)
                self.newpic = true
            }

        }


       // let camerRollAction=UIAlertController(title:"",message:"",preferredStyle: .actionSheet)

        let camerRollAction=UIAlertAction(title:"Gallery",style:.default){(action) in

            if UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.photoLibrary) {

                let imagepicker = UIImagePickerController()
                imagepicker.delegate = self
                imagepicker.sourceType = UIImagePickerControllerSourceType.photoLibrary
                imagepicker.mediaTypes = [kUTTypeImage as! String]
                imagepicker.allowsEditing = false

                self.present(imagepicker, animated: true, completion: nil)
                self.newpic = true
            }

        }

        myalert.addAction(camerAction)
        myalert.addAction(camerRollAction)

        self.present(myalert, animated: true, completion: nil)

    }



    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {


        let mediatype = info[UIImagePickerControllerMediaType] as! NSString


        if mediatype.isEqual(to: kUTTypeImage as! String) {

            let image=info[UIImagePickerControllerOriginalImage] as! UIImage

            displayPhotoImageView.image = image

            let imageData:NSData = UIImagePNGRepresentation(image)! as NSData

           data = imageData.base64EncodedString(options: .lineLength64Characters)

            if newpic == true {

                UIImageWriteToSavedPhotosAlbum(image, self, #selector(imageError), nil)
            }
        }

        self.dismiss(animated: true, completion: nil)
    }

    func imageError(image:UIImage,didFinishSavingWithError error:NSErrorPointer,ContextInfo:UnsafeRawPointer){

        if error != nil {

            let alert=UIAlertController(title: "error ", message: " unable ", preferredStyle: .alert)

            let ok=UIAlertAction(title: "ok", style: .cancel, handler:nil )

            alert.addAction(ok)
            self.present(alert, animated: true, completion: nil)

        }

    }


    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        self.dismiss(animated: true, completion: nil)
    }

您需要“隐私-相机使用说明”和“隐私-照片库使用说明”权限