在我的应用程序中,我允许用户将其图像上传到我的亚马逊s3存储桶中。一旦用户从手机上捕捉到照片,我就将其显示在屏幕上,然后开始上传。此时,我开始显示进度条,它告诉用户现在是什么体育场。但是,有没有一种方法可以在上传图像时使其灰显,并在上传完成时恢复原始颜色?
到目前为止,我的代码如下:
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
let path = NSTemporaryDirectory().stringByAppendingString("image.jpeg")
if let data = UIImageJPEGRepresentation(image, 0.8) {
data.writeToFile(path, atomically: true)
}
self.dismissViewControllerAnimated(true, completion: {})
let credentialsProvider = AWSCognitoCredentialsProvider(regionType:CognitoRegionType,
identityPoolId:CognitoIdentityPoolId)
let configuration = AWSServiceConfiguration(region:CognitoRegionType, credentialsProvider:credentialsProvider)
AWSServiceManager.defaultServiceManager().defaultServiceConfiguration = configuration
let ext = "jpeg"
let uploadRequest = AWSS3TransferManagerUploadRequest()
uploadRequest.body = NSURL(string: "file://"+path)
uploadRequest.key = NSProcessInfo.processInfo().globallyUniqueString + "." + ext
uploadRequest.bucket = S3BucketName
uploadRequest.contentType = "image/" + ext
//here I would like to present a greyed out photo until it's fully uploaded:
imageView.image = image
progressBar.hidden = false
uploadRequest.uploadProgress = { (bytesSent, totalBytesSent, totalBytesExpectedToSend) -> Void in
dispatch_async(dispatch_get_main_queue(), { () -> Void in
if totalBytesExpectedToSend > 0 {
self.progressBar.progress = Float(Double(totalBytesSent) / Double(totalBytesExpectedToSend))
}
})
}
let transferManager = AWSS3TransferManager.defaultS3TransferManager()
transferManager.upload(uploadRequest).continueWithBlock { (task) -> AnyObject! in
if let error = task.error {
print("Upload failed ❌ (\(error))")
}
if let exception = task.exception {
print("Upload failed ❌ (\(exception))")
}
if task.result != nil {
let s3URL = NSURL(string: "http://s3-eu-west-1.amazonaws.com/\(S3BucketName)/\(uploadRequest.key!)")!
//print("Uploaded to:\n\(s3URL)")
self.photoURL = s3URL.absoluteString
print(self.photoURL)
}
else {
print("Unexpected empty result.")
}
return nil
}
}发布于 2018-05-15 15:12:40
下面的类别将允许在不更改初始图像的情况下将您的图像转换为黑白(不确定在您的情况下‘灰色’到底意味着什么,但如果您可以很容易地尝试)。(Swift 4)
extension UIImage {
var grayed: UIImage {
guard let ciImage = CIImage(image: self)
else { return self }
let filterParameters = [ kCIInputColorKey: CIColor.white, kCIInputIntensityKey: 1.0 ] as [String: Any]
let grayscale = ciImage.applyingFilter("CIColorMonochrome", parameters: filterParameters)
return UIImage(ciImage: grayscale)
}
}发布于 2016-03-12 23:21:52
有多种方法可以做到这一点。一种简单的方法是在图像视图的顶部放置一个用opaque=false设置的UIView,背景颜色为深灰色,Alpha值为50%左右。这会使图像变暗,使其看起来暗淡和低对比度。
也可以在图像视图下放置100%不透明的黑色UIView,然后将图像视图上的alpha设置为50%。
您可以在图像视图的图层上放置一个CALayer,其背景颜色为50%不透明的深灰色。
所有这些方法都会产生类似的效果。
注意,您还可以将图像视图放在为UIBlurEffect设置的UIVisualEffectView的content视图中。这会使你的图像变得模糊,而不是变灰。
https://stackoverflow.com/questions/35959378
复制相似问题