Thursday, November 27, 2014

iOS Architecture - Cocoa Stack

APP

Cocoa Touch - UIKit

Media

Core Services - Foundation

Core OS


=====================

Link:
http://www.cnblogs.com/kkun/archive/2012/04/25/2470166.html


=====================

UIKit:
Prefix: UI
Examples: UIApplication, UIWindow, UIScreen, UIView, UIViewController...etc.

Foundation:
Prefix: NS
Examples: NSData, NSDate, NSInteger, NSArray, NSLog, ...etc.

Monday, November 24, 2014

CIFilter - Invert the photo color by built-in filter

//Updated December 22, 2014:
//Keep the photo orientation.

//Updated December 30, 2014
//Remove the error: BSXPCMessage received error for message: Connection interrupted
//Modify this: CIContext(options:[kCIContextUseSoftwareRenderer: true])


import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var picture: UIImageView!
    
    @IBAction func photoAlbum(sender: AnyObject) {
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)) {
            var pickerController = UIImagePickerController()
            pickerController.delegate = self
            self.presentViewController(pickerController, animated: true, completion: nil)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        
        let uiPictureSelected = info[UIImagePickerControllerOriginalImage] as UIImage
        let myOrientation : UIImageOrientation = uiPictureSelected.imageOrientation //Keep the photo orientation.
        let ciPictureSelected = CIImage(image:uiPictureSelected)

        var myFilter = CIFilter(name: "CIColorInvert")
        myFilter.setValue(ciPictureSelected, forKey: kCIInputImageKey)
        
        let ciOutputImage = myFilter.outputImage
        
        let ciContext = CIContext(options:[kCIContextUseSoftwareRenderer: true])
        let cgOutputImage = ciContext.createCGImage(ciOutputImage, fromRect: ciOutputImage.extent())
        
        let uiOutputImage = UIImage(CGImage: cgOutputImage, scale: 1, orientation: myOrientation)
        self.picture.image = uiOutputImage
        
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.

    }

Tuesday, November 18, 2014

UIImagePickerControllerSourceType.Camera - Take a photo

1. Drag a UIImageView and Button to the storyboard.
Adjust the view mode of UIImageView to Aspect Fit

2. Control-drag to create:

    @IBOutlet weak var picture: UIImageView!
    
    @IBAction func takePhoto(sender: AnyObject) {

    }

3. Add delegates

class ViewController: UIViewControllerUIImagePickerControllerDelegateUINavigationControllerDelegate {



4. Complete Code:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var picture: UIImageView!
    
    @IBAction func takePhoto(sender: AnyObject) {
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)) {
            var pickerController = UIImagePickerController()
            pickerController.delegate = self
            pickerController.sourceType = UIImagePickerControllerSourceType.Camera
            self.presentViewController(pickerController, animated: true, completion: nil)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        let pictureTaken = info[UIImagePickerControllerOriginalImage] as UIImage
        self.picture.image = pictureTaken
        UIImageWriteToSavedPhotosAlbum(pictureTaken, nil, nil, nil)
        picker.dismissViewControllerAnimated(true, completion: nil)
    }

Monday, November 17, 2014

presentViewController(pickerController - Show a photo from the photo album

Updated with Xcode 7.1/Swift 2.1 (November 11, 2015):

Edit ViewController.swift as below:


import UIKit

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    var imageView : UIImageView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
       
        let button = UIButton(frame: CGRectMake(70, 100, 200, 20))
        button.setTitle("Pick a Photo", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blueColor(), forState: UIControlState.Normal)
        button.setTitleColor(UIColor.cyanColor(), forState: UIControlState.Highlighted)
        button.addTarget(self, action: "photoAlbum:", forControlEvents: UIControlEvents.TouchUpInside)
        view.addSubview(button)
        
        imageView = UIImageView(frame: CGRectMake(50, 150, 200, 200))
        imageView.contentMode = UIViewContentMode.ScaleAspectFit
        view.addSubview(imageView)
    }
    
    func photoAlbum(sender: UIButton) {
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)) {
            let pickerController = UIImagePickerController()
            pickerController.delegate = self
            self.presentViewController(pickerController, animated: true, completion: nil)
        }
    }
    
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        let pictureSelected = info[UIImagePickerControllerOriginalImage] as! UIImage
        imageView.image = pictureSelected
        picker.dismissViewControllerAnimated(true, completion: nil)
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

}

Original Post (November 17, 2014):

1. Drag a UIImageView and Button to the storyboard.
Adjust the view mode of UIImageView to Aspect Fit

2. Control-drag to create:

    @IBOutlet weak var picture: UIImageView!
    
    @IBAction func photoAlbum(sender: AnyObject) {


    }

3. Add delegates

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

4. Complete Code:

class ViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate {

    @IBOutlet weak var picture: UIImageView!
    
    @IBAction func photoAlbum(sender: AnyObject) {
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.SavedPhotosAlbum)) {
            var pickerController = UIImagePickerController()
            pickerController.delegate = self
            self.presentViewController(pickerController, animated: true, completion: nil)
        }
    }
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        let pictureSelected = info[UIImagePickerControllerOriginalImage] as UIImage
        self.picture.image = pictureSelected
        picker.dismissViewControllerAnimated(true, completion: nil)
    }