我已经成功地将静态图像上传到AWS服务器。当我把它和图像转换器结合在一起的时候,我面临的问题是同一个图像被上传到AWS,尽管我选择和命名它们的方式不同。守则如下:
internal func imagePickerController(_ picker: UIImagePickerController,
didFinishPickingMediaWithInfo info: [String : Any])
{
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
var imageUrl = info[UIImagePickerControllerReferenceURL] as? NSURL
let imageName = imageUrl?.lastPathComponent
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)
print("image name : \(imageName)")
if !FileManager.default.fileExists(atPath: localPath!.path) {
do {
try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
print("file saved")
//let imageData = NSData(contentsOf: localPath!)
//let finalURL = localPath!
//this is in swift 2; above 2 lines are its equivalent in swift3. I think the problem lies here
//let imageData = NSData(contentsOfFile: localPath)!
//imageURL = NSURL(fileURLWithPath: localPath)
}catch {
print("error saving file")
}
}
else {
print("file already exists")
}
self.dismiss(animated: true, completion: nil)
let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: "identity pool id")
let configuration = AWSServiceConfiguration(region: .APSoutheast1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
//these are the static values I used that worked perfectly fine with separate images
//let localFileName = "Alerts_bg"
//let ext = "png"
//let remoteName = localFileName + "." + ext
//let imageURL = Bundle.main.url(forResource: localFileName, withExtension: ext)!
let transferManager = AWSS3TransferManager.default()
let uploadRequest = AWSS3TransferManagerUploadRequest()!
uploadRequest.bucket = "bucket"
let imageAWSName = "ios_" + NSUUID().uuidString + ".jpg"
uploadRequest.key = imageAWSName
uploadRequest.body = localPath! as URL
uploadRequest.contentType = "image/jpg"
print("req123 : \(uploadRequest)")
uploadRequest.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
DispatchQueue.main.async(execute: {
//self.amountUploaded = totalBytesSent // To show the updating data status in label.
//self.fileSize = totalBytesExpectedToSend
print("progress : \(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
if let error = task.error {
print("Upload failed with error: (\(error.localizedDescription))")
}
if task.result != nil {
let s3URL = URL(string: "https://s3-ap-southeast-1.amazonaws.com/bucket/\(imageAWSName)")!
print("Uploaded to:\(s3URL)")
}
return nil
})
dismiss(animated:true, completion: nil) //5
}我见过很多博客,如这和这,但这些博客都是早期版本的swift,我无法在Swift 3中转换它,并将图像处理器与AWS正确地结合起来。谁来帮帮忙。
发布于 2017-06-03 07:04:26
我找到了解决问题的办法:
let imageAWSName = "ios_" + NSUUID().uuidString + ".jpg"
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first!
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageAWSName)
if !FileManager.default.fileExists(atPath: localPath!.path) {
do {
try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!)
print("file saved")
}catch {
print("error saving file")
}
}
else {
print("file already exists")
}
let credentialProvider = AWSCognitoCredentialsProvider(regionType: .USEast1, identityPoolId: “your identity pool id”)
let configuration = AWSServiceConfiguration(region: .APSoutheast1, credentialsProvider: credentialProvider)
AWSServiceManager.default().defaultServiceConfiguration = configuration
let transferManager = AWSS3TransferManager.default()
let uploadRequest = AWSS3TransferManagerUploadRequest()!
let yourBucketName = “your bucket name”
uploadRequest.bucket = yourBucketName
uploadRequest.key = imageAWSName
uploadRequest.body = localPath! as URL
uploadRequest.contentType = "image/jpg"
uploadRequest.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
DispatchQueue.main.async(execute: {
//self.amountUploaded = totalBytesSent // To show the updating data status in label.
//self.fileSize = totalBytesExpectedToSend
print("progress : \(totalBytesSent)/\(totalBytesExpectedToSend)")
})
}
transferManager.upload(uploadRequest).continueWith(executor: AWSExecutor.mainThread(), block: { (task:AWSTask<AnyObject>) -> Any? in
if let error = task.error {
print("Upload failed with error: (\(error.localizedDescription))")
}
if task.result != nil {
let s3URL = URL(string: "https://s3-ap-southeast-1.amazonaws.com/\(yourBucketName)/\(imageAWSName)")!
print("Uploaded to:\(s3URL)")
}
return nil
})
self.picker.dismiss(animated: true, completion: nil)确保在localPath中使用的localPath总是与我所做的不同。这是主要的事情,否则AWS将多次保存相同的图像,即使您从选择器中选择不同的图像。
希望这对将来的人有帮助!
发布于 2017-05-30 08:02:43
实际上,当你这样做的时候:
let photoURL = NSURL(fileURLWithPath: documentDirectory)
let localPath = photoURL.appendingPathComponent(imageName!)您要将localPath创建为URL。
所以当你在做:
let imageURL = NSURL(fileURLWithPath: localPath)这造成了错误,因为localPath是URL而不是String。在这里,您可以直接使用:
let imageURL = localPath!https://stackoverflow.com/questions/44255317
复制相似问题