Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何选择可以更改的图片(如个人资料图片)?_Ios_Swift_Xcode - Fatal编程技术网

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

Ios 如何选择可以更改的图片(如个人资料图片)?,ios,swift,xcode,Ios,Swift,Xcode,一旦我从“多媒体资料”中选择了一张图片到imageView,图像的大小就会因某种原因而改变。当我离开这个称呼,再次回来时,照片就缩小了 这就是没有图片的开头 这就是我们在选择图片后的看法 这就是我离开应用程序并从应用程序返回后的样子 我不知道为什么会这样。我希望这张照片是一张个人资料照片,它看起来不太好 这是我的密码- class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationC

一旦我从“多媒体资料”中选择了一张图片到imageView,图像的大小就会因某种原因而改变。当我离开这个称呼,再次回来时,照片就缩小了

这就是没有图片的开头

这就是我们在选择图片后的看法

这就是我离开应用程序并从应用程序返回后的样子

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

这是我的密码-

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)
    }

}
类ViewController:UIViewController、UIImagePickerControllerDelegate、UINavigationControllerDelegate{
@IBVAR imageView:UIImageView!
@IBOUTLE var chooseBuuton:UIButton!
var imagePicker=UIImagePickerController()
@iAction func btnClicked(){
如果UIImagePickerController.isSourceTypeAvailable(.savedPhotosAlbum){
打印(“按钮捕获”)
imagePicker.delegate=self
imagePicker.sourceType=.savedPhotosAlbum;
imagePicker.allowsEditing=false
自我呈现(imagePicker,动画:true,完成:nil)
}
}
func imagePickerController(picker:UIImagePickerController!,didFinishPickingImage:UIImage!,编辑信息:NSDictionary!){
disclose(动画:true,完成:{()->Void in
})
imageView.image=image
}
func webView加载(webView:UIWebView)
{
UIApplication.shared.isNetworkActivityIndicatorVisible=true
}
func webViewDidFinishLoad(webView:UIWebView)
{
UIApplication.shared.isNetworkActivityIndicatorVisible=false
}
重写func viewDidLoad(){
super.viewDidLoad()
//加载视图后,通常从nib执行任何其他设置。
让nsDocumentDirectory=FileManager.SearchPathDirectory.documentDirectory
让nsUserDomainMask=FileManager.SearchPathDomainMask.userDomainMask
让path=NSSearchPathForDirectoriesInDomains(nsDocumentDirectory,nsUserDomainMask,true)
如果让dirPath=path.first
{
让imageURL=URL(fileURLWithPath:dirPath)。appendingPathComponent(“image.png”)
让image=UIImage(contentsOfFile:imageURL.path)
imageView.image=image
}否则{
//图像不存在
//在这里你想做什么就做什么
}
}
func ImagePickerController IDCancel(picker:UIImagePickerController){
驳回(动画:真,完成:无)
}
func imagePickerController(picker:UIImagePickerController,didFinishPickingMediaWithInfo:[字符串:任意]){
如果让image=info[UIImagePickerControllerOriginalImage]作为?UIImage{
imageView.image=image
imageView.contentMode=.ScaleSpectFill
让path=try!FileManager.default.url(适用于:FileManager.SearchPathDirectory.documentDirectory,在:FileManager.SearchPathDomainMask.userDomainMask中,适用于:nil,创建:false)
让newPath=path.appendingPathComponent(“image.png”)//您可能可以使用用户名轻松获取用户信息
让jpgImageData=UIImageJPEGRepresentation(图像,1.0)
做{
尝试jpgImageData!.write(到:newPath)
}抓住{
打印(错误)
}
}
驳回(动画:真,完成:无)
}
覆盖func TouchesBegind(Touchs:Set,带有事件:UIEvent?){
let controller=UIImagePickerController()
controller.delegate=self
controller.sourceType=.photoLibrary
存在(控制器,动画:真,完成:无)
}
}

在两个位置设置
clipstobunds
contentMode
属性:

视图加载中

    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
    }
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBound = true
中使用信息完成了PickingMediaWithInfo

    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
    }
    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage {
        imageView.image = image
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBound = true

我得到“类型'UIImageView'的值没有成员'clipstobounds'”尝试为所有位置编写
imageView.contentMode=.scaleAspectFill
,在这些位置,您可以为imageViewimageView设置图像。clipstobounds=true,imageView.layer.masksToBounds=trueset从情节提要。选择imageview go属性检查器,然后检查要绑定的剪辑。*viewDidLoad yes?*两次写入同一内容的好处是什么。如果他想从代码中设置,那么他应该使用代码来编写,他可以从情节提要中设置,也可以在
viewDidLoad
中从非常初始的位置设置
ClipToBound