首页 > 解决方案 > 为什么 UIImagePickerController 中的图像尺寸大于原始尺寸?

问题描述

我试图在 这里阅读类似的问题,但它不能解决我的问题,因为这里的解决方案已被弃用。

所以我有一张图片,画廊的大小只有 57 Kb。我希望用户从他们的图库中挑选一张图片,但问题是,从图库中挑选的图片远大于磁盘中的原始图片,从 57 Kb 变为 560 Kb。这是我使用的代码

   func showChooseImageFromGalleryAlert() {
        let imagePickerController = UIImagePickerController()
        imagePickerController.delegate = self
        imagePickerController.allowsEditing = false
        
        let actionSheet = UIAlertController(title: "Choose Poster", message: "Choose your best poster", preferredStyle: .actionSheet)
        
        let actionPhotoLibrary = UIAlertAction(title: "Choose From Gallery gallery", style: .default) { (action) in
            imagePickerController.sourceType = .photoLibrary
            imagePickerController.modalPresentationStyle = .fullScreen
            self.present(imagePickerController, animated: true, completion: nil)
        }
        
        let actionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)
        
        actionSheet.addAction(actionPhotoLibrary)
        actionSheet.addAction(actionCancel)
        
        actionSheet.view.tintColor = UIColor(named: ColorName.BRAND_COLOR)
        
        self.present(actionSheet, animated: true, completion: nil)
    }

结果在这里

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
        
        
        guard let image = info[.originalImage] as? UIImage  else {
            picker.dismiss(animated: true, completion: nil)
            return
        }
        
        print("check size in Kb: \(getImageSizeInkB(image: image)!) Kb")

        // the image is far larger than original image in here
        // it becomes 560 Kb. 10 times bigger
        
        
        picker.dismiss(animated: true, completion: nil)
        
       
        
        
    }

我可以得到与原始图像相同的尺寸吗?

标签: iosswiftuiimagepickercontroller

解决方案


推荐阅读