gpt4 book ai didi

ios - 尝试从手机库中选择全景图像时 UIImagePickerController 崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 05:37:36 24 4
gpt4 key购买 nike

我正在尝试使用 UIImagePickerController 从设备库中选择一个图像,一切正常,除非我选择一个非常大的图像(全景图像),应用程序崩溃而没有留下任何痕迹。我认为这是一个内存问题(我注意到内存使用量增加到 600m)。所以我的问题是我可以指定图像大小作为过滤器吗?或者我应该创建我的自定义图像选择器?我还使用 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)
}
}

编辑:我检查了视频持续时间的问题,但这不是我需要的,我们没有同样的问题,这里我谈谈从图像选择器中选择图像,你可以在推特上试试应用程序并附加一个巨大的全景总部图像。该问题建议讨论限制录制视频的持续时间。

最佳答案

应用程序因图像过大而崩溃。您可以使用以下函数调整图像大小。此函数将返回图像。如果您需要数据,则取消注释这两行并返回数据而不是图像。

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)

})

关于ios - 尝试从手机库中选择全景图像时 UIImagePickerController 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35047690/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com