gpt4 book ai didi

ios - 如何在 swift 中暂时使我的 uiImage 变灰?

转载 作者:行者123 更新时间:2023-11-29 01:09:16 26 4
gpt4 key购买 nike

在我的应用程序中,我让用户将他的图像上传到我的 amazon 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
}


}

最佳答案

下面的类别将允许将您的图像变成黑白图像,而无需更改初始图像(不确定您的情况下“灰色”到底意味着什么,但您可以轻松尝试)。( 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)
}
}

关于ios - 如何在 swift 中暂时使我的 uiImage 变灰?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35959378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com