Andras Danyi
Andras Danyi

Reputation: 21

Swift images get stretched

So I am totally new to programming and swift, this is my second week of trying to code. A lot of fun but a lot of errors as well. So I want to make an app where the user can choose a photo from their gallery or make a photo using there camera, and after a press of a button, this image will get pixalised(using the Core Image function).

The problem is whenever I press the button, the image seems to get stretched, and I can't figure out why. After browsing a picture:

s8.postimg.org/4ppibhudh/Screen_Shot_2015_07_28_at_00_14_12.png

After pressing the button:

s8.postimg.org/d98w992px/Screen_Shot_2015_07_28_at_00_14_28.png

Thanks for any answers!

My code is as follows:

import UIKit

class ViewController: UIViewController,UIImagePickerControllerDelegate,UINavigationControllerDelegate {


    @IBOutlet weak var myImageView: UIImageView!

    let picker = UIImagePickerController()

    func noCamera(){
        let alertVC = UIAlertController(title: "No Camera", message: "Don't try it on a computer Dumbass!", preferredStyle: .Alert)
        let okAction = UIAlertAction(title: "Sorry about that :(", style:.Default, handler: nil)
        alertVC.addAction(okAction)
        presentViewController(alertVC, animated: true, completion: nil)
    }

    @IBAction func photofromLibrary(sender: UIBarButtonItem) {
        picker.allowsEditing = false //2
        picker.sourceType = .PhotoLibrary //3
        picker.modalPresentationStyle = .Popover
        presentViewController(picker, animated: true, completion: nil)//4
        picker.popoverPresentationController?.barButtonItem = sender
    }

    @IBAction func shootPhoto(sender: UIButton) {
        if UIImagePickerController.availableCaptureModesForCameraDevice(.Rear) != nil {
            picker.allowsEditing = false
            picker.sourceType = UIImagePickerControllerSourceType.Camera
            picker.cameraCaptureMode = .Photo
            presentViewController(picker, animated: true, completion: nil)
        } else {
            noCamera()
        }

    }


    @IBAction func pixelise(sender: UIButton) {

        // 1
        let ciImage = CIImage(image: myImageView.image)


        // 2
        var filter = CIFilter(name: "CIPixellate")
        filter.setDefaults()
        filter.setValue(ciImage, forKey: kCIInputImageKey)
        myImageView.contentMode = .ScaleAspectFit

        // 3
        var outputImage = filter.outputImage
        var newImage = UIImage(CIImage: outputImage)
        myImageView.image = newImage


    }



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


    }
    //MARK: Delegates
    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [NSObject : AnyObject]) {
        var chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage //2
        myImageView.contentMode = .ScaleAspectFit //3
        myImageView.image = chosenImage //4
        dismissViewControllerAnimated(true, completion: nil) //5


    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        dismissViewControllerAnimated(true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


}

Upvotes: 1

Views: 386

Answers (1)

Rob
Rob

Reputation: 437552

The process of converting CIImage to UIImage consists of creating a CIContext, then creating a CGImage using that context, and then creating a UIImage from that:

// 1
let ciImage = CIImage(image: image)

// 2
let filter = CIFilter(name: "CIPixellate")
filter.setDefaults()
filter.setValue(ciImage, forKey: kCIInputImageKey)

// 3
let context = CIContext(options: nil)
let cgImage = context.createCGImage(filter.outputImage, fromRect: CGRect(x: 0, y: 0, width: image.size.width, height: image.size.height))
let outputImage = UIImage(CGImage: cgImage)

That yields:

enter image description here

Upvotes: 1

Related Questions