您的位置:首页 > 移动开发 > Swift

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

2016-11-02 00:00 393 查看
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)
}
})
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐