如何选择可以更改的个人资料图片?

时间:2018-04-09 12:02:02

标签: ios swift xcode

从图库中选择图片 imageView 后,图片的大小会因某种原因发生变化。当我离开产区并再次回来时,图片会缩小。

如果没有图片,那么开头看起来如何

enter image description here

这是我们选择图片的方式

enter image description here

我离开并从应用程序回来后,我的照顾方式

enter image description here

我不知道为什么会这样。我希望这张照片是一张个人资料图片,看起来并不那么好。

这是我的代码 -

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet var imageView: UIImageView!
    @IBOutlet var chooseBuuton: UIButton!
    var imagePicker = UIImagePickerController()

    @IBAction func btnClicked() {

        if UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
            print("Button capture")

            imagePicker.delegate = self
            imagePicker.sourceType = .savedPhotosAlbum;
            imagePicker.allowsEditing = false

            self.present(imagePicker, animated: true, completion: nil)
        }
    }


    func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!){
        self.dismiss(animated: true, completion: { () -> Void in

        })

        imageView.image = image
    }
    func webViewDidStartLoad(_ webView: UIWebView)
    {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }

    func webViewDidFinishLoad(_ webView: UIWebView)
    {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()


        // Do any additional setup after loading the view, typically from a nib.
        let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
        let nsUserDomainMask    = FileManager.SearchPathDomainMask.userDomainMask
        let paths               = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
        if let dirPath          = paths.first
        {
            let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("image.png")
            let image    = UIImage(contentsOfFile: imageURL.path)
            imageView.image = image
        }else{

            // Image not present
            // Do whatever you want to do here
        }
    }

    func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
        dismiss(animated: true, completion: nil)
    }
    func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

        if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
            imageView.image = image
            imageView.contentMode = .scaleAspectFill
            let path = try! FileManager.default.url(for: FileManager.SearchPathDirectory.documentDirectory, in: FileManager.SearchPathDomainMask.userDomainMask, appropriateFor: nil, create: false)
            let newPath = path.appendingPathComponent("image.png") //Possibly you can Use the UserName to fetch easily User-wise
            let jpgImageData = UIImageJPEGRepresentation(image, 1.0)
            do {
                try jpgImageData!.write(to: newPath)
            } catch {
                print(error)
            }
        }
        dismiss(animated: true, completion: nil)
    }


    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

        let controller = UIImagePickerController()
        controller.delegate = self
        controller.sourceType = .photoLibrary
        present(controller, animated: true, completion: nil)
    }

}

1 个答案:

答案 0 :(得分:1)

在两个地方设置clipstoboundscontentMode属性:

viewDidLoad

    if let dirPath = paths.first {
        let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("image.png")
        let image = UIImage(contentsOfFile: imageURL.path)
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBound = true
    } else {
        // Image not present
        // Do whatever you want to do here
    }

didFinishPickingMediaWithInfo

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBound = true