简体   繁体   中英

iOS: CIColorInvert filter is not working on device

This is my code block to invert colors of an image:

import Foundation
import UIKit
import CoreImage

extension UIImage {
    static let defaultImage = UIImage(systemName: "person.circle.fill")!
    func colorInverted() -> UIImage {
        guard let ciImage = CIImage(image: self) else {
            print("UIImage: Failed to get CIImage!")
            return UIImage.defaultImage
        }
        guard let filter = CIFilter(name: "CIColorInvert") else {
            print("UIImage: Failed to get filter!")
            return UIImage.defaultImage
        }
        filter.setValue(ciImage, forKey: kCIInputImageKey)
        guard let image = filter.outputImage else {
            print("UIImage: Failed to get output image!")
            return UIImage.defaultImage
        }
        return UIImage(ciImage: image)
    }
}

This is working fine on simulator:

在模拟器上

But on real device the result is a full blue image:

在真实设备上

I have searched for a while but could not able to find a solution. Can anyone help?

Note: I am getting images from GitHub API:

https://avatars0.githubusercontent.com/u/1?v=4

use CIContext instead of UIImage(CIImage:)

func applyFilter(){
    let image = CIImage(CGImage: myimage.image?.CGImage)
    let filter = CIFilter(name: "CISepiaTone")
    filter.setDefaults()
    filter.setValue(image, forKey: kCIInputImageKey)

    let context = CIContext(options: nil)
    let imageRef = context.createCGImage(filter.outputImage, fromRect: image.extent())
    myimage.image = UIImage(CGImage: imageRef)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM