我怎么能在swift中暂时使我的uiImage变灰?

时间:2016-03-12 15:12:05

标签: ios swift uiimageview uiimage

在我的应用中,我让用户将他的图片上传到我的亚马逊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
    }


}

3 个答案:

答案 0 :(得分:1)

有多种方法可以做到这一点。一种简单的方法是将UIView放在图像视图的顶部,使用opaque = false设置,背景颜色为深灰色,alpha值为50%左右。这会使图像变暗并使其看起来暗淡且对比度低。

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

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

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

请注意,您还可以将图片视图放在UIBlurEffect的UIVisualEffectView集的内容视图中。这会模糊你的形象,而不是让它变灰。

答案 1 :(得分:0)

let image = UIImage(named: "image")?.withRenderingMode(.alwaysTemplate)
let imageView = UIImageView(image: image)
imageView.tintColor = UIColor.gray

答案 2 :(得分:0)

下面的类别可以让你的图像变成黑白而不改变初始图像(不知道你的情况下'灰色'究竟是什么意思,但你可以轻松地尝试它)。 (斯威夫特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)
    }
}