Reputation: 3484
I want to create an image filtering app that applies a random filter to an image in an Image View. I followed a 2014 Apple tutorial. As per Xcode errors and warnings, now I have the following code in ViewController.swift.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var photoImageView: UIImageView!
// Create a place to render the filtered image
let context = CIContext(options: nil)
@IBAction func applyFilter(_ sender: Any) {
// Create an image to filter
let inputImage = CIImage(image: photoImageView.image!)
// Create a random color to pass to a filter
let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]
// Apply a filter to the image
let filteredImage = inputImage?.applyingFilter("CIHueAdjust", parameters: randomColor)
// Render the filtered image
let renderedImage = context.createCGImage(filteredImage!, from: filteredImage!.extent)
// Reflect the change back in the interface
//photoImageView.image = UIImage(CGImage: renderedImage!)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The code before the last commented statement is successfully built. I had to comment the statement because it gave the error Ambiguous use of init(CGImage)
. The code has no effect currently. It just shows an Image View and a bar button below it (clicking the button does not do anything). Note that my code is slightly different from that of the tutorial, as the tutorial code seems deprecated. Any ideas why the code does not work?
The following is the code from the original tutorial:
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var photoImageView: UIImageView!
// Create a place to render the filtered image
let context = CIContext(options: nil)
@IBAction func applyFilter(sender: AnyObject) {
// Create an image to filter
let inputImage = CIImage(image: photoImageView.image)
// Create a random color to pass to a filter
let randomColor = [kCIInputAngleKey: (Double(arc4random_uniform(314)) / 100)]
// Apply a filter to the image
let filteredImage = inputImage.imageByApplyingFilter("CIHueAdjust", withInputParameters: randomColor)
// Render the filtered image
let renderedImage = context.createCGImage(filteredImage, fromRect: filteredImage.extent())
// Reflect the change back in the interface
photoImageView.image = UIImage(CGImage: renderedImage)
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Upvotes: 0
Views: 1920
Reputation: 19641
Your code shouldn't build.
Once you fix the core image filter name, try replacing:
photoImageView.image = UIImage(CGImage: renderedImage!)
with:
photoImageView.image = UIImage(cgImage: renderedImage!)
// ^
// |
// attention to capitalization
Upvotes: 1
Reputation: 1270
There is typo in core image filter name. Also try to run your code in device.
Upvotes: 0