在 iOS/swift 中上传图像时如何删除裁剪选项

Posted

技术标签:

【中文标题】在 iOS/swift 中上传图像时如何删除裁剪选项【英文标题】:How to remove cropping option when uploading an image in iOS/ swift 【发布时间】:2019-04-24 07:27:29 【问题描述】:

这是带有裁剪选项的图像。

我想删除此裁剪选项并想上传全长图像。是否可以使用 ios 图像选择器?

当我要从照片库上传图片时,它会显示裁剪选项。我不想要裁剪选项。我想上传完整图像而不进行裁剪。如何删除该裁剪选项?是否可以?请帮忙。 我使用了以下代码,但它不起作用:

 @IBAction func clickImageUpload(_ sender: Any)


    let actionSheet = UIAlertController(title: nil, message: "Choose an Option", preferredStyle: .actionSheet)

    actionSheet.addAction(UIAlertAction(title: "Choose from Gallery", style: .default, handler: 
        (alert: UIAlertAction!) -> Void in

        self.imagePickerController.allowsEditing = true
        self.imagePickerController.sourceType = UIImagePickerControllerSourceType.photoLibrary
        self.imagePickerController.delegate = self

        self.present(self.imagePickerController, animated: true, completion: nil)
    ))

    actionSheet.addAction(UIAlertAction(title: "Take a Photo", style: .default, handler: 
        (alert: UIAlertAction!) -> Void in
        if UIImagePickerController.isSourceTypeAvailable(.camera) 
            self.imagePickerController.allowsEditing = true
            self.imagePickerController.sourceType = UIImagePickerControllerSourceType.camera
            self.imagePickerController.delegate = self

            self.present(self.imagePickerController, animated: true, completion: nil)
        
        else
            print("No camera")
        


    ))



    actionSheet.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))

    self.present(actionSheet, animated: true, completion: nil)



func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String: Any]) 


    let image = info[UIImagePickerControllerEditedImage] as! UIImage

        imgvwProfile.contentMode = .scaleAspectFit
        imgvwProfile.image = image.squareMe()

    //imgvwProfile.image = image
    self.imageData = UIImageJPEGRepresentation(self.imgvwProfile.image!, 1.0)!
    print(imageData)
   print(info)
    //obtaining saving path
    if let assetPath = info[UIImagePickerControllerReferenceURL] 

    if ((assetPath as! NSURL).absoluteString?.hasSuffix("JPG"))! 
        print("JPG")
        extensionType = "jpg"
    
    else if ((assetPath as! NSURL).absoluteString?.hasSuffix("PNG"))! 
        print("PNG")
         extensionType = "png"
    
    else if ((assetPath as! NSURL).absoluteString?.hasSuffix("GIF"))! 
        print("GIF")
         extensionType = "gif"
    
    else 
        print("Unknown")
         //extensionType = "Unknown"
    
    

    if extensionType.count == 0 
        extensionType = "jpg"
    

    let fileManager = FileManager.default
    let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first
    print("IMAGE= \(documentsPath)")
    let imagePath = documentsPath?.appendingPathComponent("image.png")
    self.idProofDic["data"] = self.imageData as! Data
    self.idProofDic["mimeType"] = "image/\(extensionType)"
    self.idProofDic["extension"] = ".\(extensionType)"

    dismiss(animated: true, completion: nil)


func imagePickerControllerDidCancel(_ picker: UIImagePickerController) 
    dismiss(animated: true, completion: nil)

【问题讨论】:

你问的是 UIImagePickerController 的功能吗?如果是这样,它与上传图像无关。请edit您的问题更清楚地说明您在做什么以及实际问题是什么。 你能发一些图片来更清楚地了解问题吗? 我已经添加了图片。请看一看。 @rmaddy n shahnilay86 你能显示你正在使用的代码吗?您显示的图像看起来不像标准的 UIImagePicker @Paulw11 这是我的代码 【参考方案1】:

您可以使用默认控件来禁用图像裁剪。

self.imagePicker.allowsEditing = false

【讨论】:

你还必须添加以下代码行:let image = info[UIImagePickerControllerOriginalImage] as! UIImage

以上是关于在 iOS/swift 中上传图像时如何删除裁剪选项的主要内容,如果未能解决你的问题,请参考以下文章

上传时如何裁剪图像?

在iOS Swift中不使用AWS SDK在Amazon S3存储桶中上传的背景图像(背景模式)

如何选择图像的区域进行裁剪?

裁剪区域不是矩形时裁剪图像

如何从 didRegisterForRemoteNotificationsWithDeviceToken 函数 ios swift 3 中删除几乎匹配的可选要求警告?

如何在angularjs中使用ng-file上传裁剪和上传图像