迅捷的图像被拉长了

时间:2015-07-27 23:25:09

标签: swift uiimageview uiimage

所以我对编程和swift完全不熟悉,这是我尝试编码的第二周。很多乐趣,但也有很多错误。所以我想创建一个应用程序,用户可以从他们的画廊中选择一张照片或使用相机制作照片,按一下按钮后,该图像将获得像素化(使用核心图像功能)。

问题是每当我按下按钮时,图像似乎都会拉长,我无法弄清楚原因。浏览图片后:

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

按下按钮后:

s8.postimg.org/d98w992px/Screen_Shot_2015_07_28_at_00_14_28.png

感谢您的回答!

我的代码如下:

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.
    }


}

1 个答案:

答案 0 :(得分:1)

CIImage转换为UIImage的过程包括创建CIContext,然后使用该上下文创建CGImage,然后从UIImage创建// 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) 的是:

{{1}}

产量:

enter image description here