尝试从电话库中选择全景图像时,UIImagePickerController崩溃

时间:2016-01-27 20:48:10

标签: ios swift uiimagepickercontroller

我正在尝试使用UIImagePickerController从设备库中选择一个图像一切正常,除非我选择一个非常大的图像(全景图像)应用程序崩溃而不给出任何痕迹。我认为这是一个内存问题(我注意到内存使用量增加到600米)。所以我的问题是我可以指定图像大小作为过滤器吗?或者我应该创建自定义图像选择器?我也用facebook和twitter图片选择器测试了相同的图像:twitter失败...

我的代码启动图像选择器是:

var picker = UIImagePickerController()
picker.delegate = self
picker.sourceType = .PhotoLibrary
presentViewController(self.picker, animated: true, completion: nil)

并捕获所选图像:

extension MYViewController : UIImagePickerControllerDelegate,UINavigationControllerDelegate{
    func imagePickerController(
    picker: UIImagePickerController,
    didFinishPickingMediaWithInfo info: [String : AnyObject])
    {
        print("user did select an image")
        let chosenImage = info[UIImagePickerControllerOriginalImage] as! UIImage

        dismissViewControllerAnimated(true, completion: nil)

    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController) {
        //do code when cancel
        print("cancel ...")
        userDidCancelImageChoosing = true
        dismissViewControllerAnimated(true, completion: nil)
    }
}

编辑:我查看了视频时长的问题但不是我需要的问题我们没有遇到同样的问题,在这里我谈谈从您可以尝试的图像选择器中选择图像推特应用程序并附加一个巨大的全景HQ图像。提出的问题涉及限制录制视频的持续时间。

1 个答案:

答案 0 :(得分:0)

由于图像过大导致应用程序崩溃。您可以使用以下功能调整图像大小。此功能将返回图像。如果需要数据,请取消注释这两行,然后返回Data而不是image。

func compressImage(image:UIImage) -> UIImage {
    // Reducing file size to a 10th

    var actualHeight : CGFloat = image.size.height
    var actualWidth : CGFloat = image.size.width
    let maxHeight : CGFloat = 1136.0
    let maxWidth : CGFloat = 640.0
    var imgRatio : CGFloat = actualWidth/actualHeight
    let maxRatio : CGFloat = maxWidth/maxHeight

    if (actualHeight > maxHeight || actualWidth > maxWidth){
        if(imgRatio < maxRatio){
            //adjust width according to maxHeight
            imgRatio = maxHeight / actualHeight;
            actualWidth = imgRatio * actualWidth;
            actualHeight = maxHeight;
        }
        else if(imgRatio > maxRatio){
            //adjust height according to maxWidth
            imgRatio = maxWidth / actualWidth;
            actualHeight = imgRatio * actualHeight;
            actualWidth = maxWidth;
        }
        else{
            actualHeight = maxHeight;
            actualWidth = maxWidth;
            compressionQuality = 1;
        }
    }


    let rect = CGRect.init(x: 0.0, y: 0.0, width: actualWidth, height: actualHeight)

    UIGraphicsBeginImageContext(rect.size);


    image.draw(in: rect)
    let img = UIGraphicsGetImageFromCurrentImageContext();

    //        let imageData = UIImageJPEGRepresentation(img ?? UIImage.init(named: "imgPlaceHolder")!, compressionQuality);
    //
    //        UIGraphicsEndImageContext();
    //
    return img ?? image
}

使用此功能的代码

        picker.dismiss(animated: false, completion: {
            let img = self.compressImage(image: chosenImage)
            let cropViewController = TOCropViewController(image: img)
            cropViewController.delegate = self
            self.present(cropViewController, animated: false, completion: nil)

        })