简体   繁体   English

仅在用相机拍摄时如何保存照片?

[英]How to save photo only when taken with camera?

I have a view controller with UIImagePickerControllerDelegate and UINavigationControllerDelegate to pick photo. 我有一个带有UIImagePickerControllerDelegate和UINavigationControllerDelegate的视图控制器来选择照片。

But when I pick photo and then it auto save a same photo in my photo album. 但是,当我选择照片后,它会自动将同一张照片保存在我的相册中。

I guess I write this function in my code 我想我在代码中写了这个函数

UIImageWriteToSavedPhotosAlbum(photoImage!, nil, nil, nil)

so, I check sourcetype equal to camera then save the photo and pick photo in album not save the same photo again . 因此,我检查源类型等于相机,然后保存照片并在相册中选择照片,而不是再次保存同一照片。
But it doesn't work. 但这是行不通的。 what's happen? 发生了什么?
It also auto save the same photo. 它还会自动保存同一张照片。
I don't want to save the same photo? 我不想保存同一张照片吗?

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

    let uuid = NSUUID().uuidString
    let imageName:String = uuid + ".jpg"
    let documentsPath = NSHomeDirectory().appending("/Documents/\(image)/")
    let imagePath = documentsPath.appending(imageName)
    let imageUrl = URL(fileURLWithPath: imagePath)
    print("imageUrl is here:\(imageUrl)")

    photoImage = info[UIImagePickerControllerOriginalImage] as? UIImage

    if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {

        photoImage = info[UIImagePickerControllerOriginalImage] as? UIImage
        // this doesn't work!!!!  
        //UIImageWriteToSavedPhotosAlbum(photoImage!, nil, nil, nil)
    }

    //save image to local Documents
    let imageData:Data = UIImageJPEGRepresentation(photoImage!, 0.05)!
    do {
        try imageData.write(to: imageUrl,options: .atomic)
    } catch let error {
        print(error)
    }

    showLoading()

    hideLoading {

        //I have func upload image to server
        self.tableView.reloadData()
        self.dismiss(animated: true, completion: nil)
    }
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

    self.dismiss(animated: true, completion: nil)
}

You are not checking the image picker controller's source type, you are checking if the camera is available in general. 您不是在检查图像选择器控制器的信号源类型,而是在检查相机是否一般可用。

Change this: 更改此:

if (UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.camera)) {

to: 至:

if picker.sourceType == .camera {

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何保存拍摄的照片 - How to save taken photo 只有在相机拍摄时才将图像保存到照片库,如果从照片库中选择则不能 - Save images to photo library ONLY if they are taken by camera and not if chosen from photo library 如何检测照片是否来自前置摄像头 - How to detect if photo taken from front camera 如何保存用UIImagePickerController相机拍摄的照片以稍后在应用程序中显示? - How can I save a photo taken with UIImagePickerController camera to be displayed later in the app? 如何在iOS中使用拍摄的照片保存GPS数据 - How to save GPS data with a taken photo in ios 从Flex Mobile中的默认相机应用程序拍摄照片后,如何上传照片? - how can i upload a photo when it is taken from the default camera app in flex mobile? 如何在相机胶卷或刚拍摄的照片中获取照片的路径(iOS / Swift)? - How to get the path of an photo in camera roll or just taken ( iOS/Swift )? 如何检测照片库中的图像是由iPhone相机拍摄还是导入 - how to detect if image from photo library was taken by iPhone camera or imported 拍照时如何查找用户位置以及如何在UIMapView中显示和保存 - how to find users location when a photo is taken and display+save in a UIMapView 将拍摄的图像保存在照片库中 - Save taken image in photo library
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM