如何将UIImage从相机转移到另一个视图控制器?

时间:2015-12-10 15:02:38

标签: ios swift image-capture

我尝试使用相机按钮拍摄照片,并将该图像通过segue传递到另一个视图控制器进行编辑。我的代码有问题,因为当我按下相机按钮时,应用程序崩溃了。这是摄像机视图控制器的代码。我已经检查了故事板中的所有连接。

@IBAction func snapStillImage(sender: AnyObject) {

    print("snapStillImage")
    dispatch_async(self.sessionQueue, {
        // Update the orientation on the still image output video connection before capturing.

        let videoOrientation =  (self.previewView.layer as! AVCaptureVideoPreviewLayer).connection.videoOrientation

        self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo).videoOrientation = videoOrientation

        // Flash set to Auto for Still Capture
        View3.setFlashMode(AVCaptureFlashMode.Auto, device: self.videoDeviceInput!.device)



        self.stillImageOutput!.captureStillImageAsynchronouslyFromConnection(self.stillImageOutput!.connectionWithMediaType(AVMediaTypeVideo), completionHandler: {
            (imageDataSampleBuffer: CMSampleBuffer!, error: NSError!) in

            if error == nil {
                let data:NSData = AVCaptureStillImageOutput.jpegStillImageNSDataRepresentation(imageDataSampleBuffer)
                let mainpictures:UIImage = UIImage( data: data)!
                                    let orientation: ALAssetOrientation = ALAssetOrientation(rawValue: mainpictures.imageOrientation.rawValue)!



                func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {

                    if (segue.identifier == "test") {

                        let destViewController : photoEditViewController = segue.destinationViewController as! photoEditViewController


                        destViewController.mainpictures = "edit"

以下是第二个视图控制器的代码

class photoEditViewController: UIViewController {


var mainpictures = String ()


@IBOutlet var mainImage: UIImageView!



override func viewDidLoad() {


    if let image = UIImage(named: mainpictures) {
        self.mainImage.image = image
    }

感谢社区

0 个答案:

没有答案