在我的应用中,我允许用户将其图像上传到我的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
    }


}

最佳答案

您可以通过多种方式执行此操作。一种简单的方法是将UIView放置在图像视图的顶部,并设置为opaque = false,并使用深灰色的背景色,其alpha值约为50%。这会使图像变暗并使它看起来暗淡和低对比度。

您还可以在图像视图下放置100%不透明的黑色UIView,然后将图像视图上的alpha设置为50%。

您可以将CALayer放置在图像视图层的顶部,背景颜色为50%不透明的深灰色。

所有这些方法都会产生类似的效果。

请注意,您还可以将图像视图放在为UIBlurEffect设置的UIVisualEffectView的内容视图中。那会使图像模糊而不是变灰。

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

10-11 21:01