swift3.0图片保存到沙盒-从沙盒读取图片显示-从沙盒上传到服务器

前端之家收集整理的这篇文章主要介绍了swift3.0图片保存到沙盒-从沙盒读取图片显示-从沙盒上传到服务器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
guard let img = UIImage(named: "aliyun-logo-local") else {
            return
        }
        
        let imgData = UIImageJPEGRepresentation(img,1)
        let path = NSSearchPathForDirectoriesInDomains(.documentDirectory,.userDomainMask,true)[0]
        let imgPath = "\(path)/\("aliyun-logo-local.png")"
        NSData(data: imgData!).write(toFile: imgPath,atomically: true)
        
        let imgView = UIImageView()
        view.addSubview(imgView)
        imgView.frame = CGRect(x: 100,y: 100,width: 100,height: 100)
        DispatchQueue.global().async(execute: {
            let fileImage = UIImage.init(contentsOfFile: imgPath)
            DispatchQueue.main.async(execute: {
                imgView.image = fileImage
            })
        })
        
        //let fileURL = Bundle.main.url(forResource: "aliyun-logo-local",withExtension: "png")
        let searchURL : URL = URL(fileURLWithPath: imgPath)
        Alamofire.upload(
            multipartFormData: { multipartFormData in
                multipartFormData.append(searchURL,withName: "file")
                
        },to: "http://127.0.0.1/upload/upload_file.PHP",encodingCompletion: { encodingResult in
                switch encodingResult {
                case .success(let upload,_,_):
                    upload.responseJSON { response in
                        debugPrint(response)
                    }
                case .failure(let encodingError):
                    print(encodingError)
                }
        })

猜你在找的Swift相关文章