TGCameraViewController icon indicating copy to clipboard operation
TGCameraViewController copied to clipboard

- (void)saveJPGImageAtDocumentDirectory:resultBlock:failureBlock: failed to save image to path

Open lichenglu opened this issue 8 years ago • 3 comments

Hi first of all, thank you so much for doing this project, which is awesome and would save me a lot of time.

However, when I used it, I found a potential bug and not sure what have caused it: the taken/picked image is not successfully saved to Image path.

I found this issue as I implemented the cameraDidSavePhoto(assetURL:) delegate method, and tried to load the image by its returned imgURL. The path is there, however, the file is not. So I got nil for the image. Tested with print("fileExists: \(fileManager.fileExists(atPath: imageURL))"), which prints false.

Also, in the - (void)saveJPGImageAtDocumentDirectory:resultBlock:failureBlock: method, I printed the saving result by doing the following:

BOOL succeeded = [data writeToFile:filePath atomically:YES];
printf("successfully write file %d", succeeded);

The result is also negative.

I am running iOS 10 with swift, and kTGCameraOptionSaveImageToAlbum is set to false.

I would really appreciate it if I missed something and you could point it out. Thank you!

lichenglu avatar Oct 22 '16 04:10 lichenglu

Hi! I couldn't understand exactly how to reproduce the issue. Does the method always return with a wrong url? Can you give me the steps to reproduce it?

macecchi avatar Oct 22 '16 20:10 macecchi

Hi macecchi, thanks for the reply!

Sorry I did not make myself clear. So the problems I think are:

  1. Currently, the assetURL for the image is not a file URL, which does not have the "file://" prefix protocol. Because the assetURL was initialised with NSURL(URLWithString:)
  2. The image is NOT saved to the "../Document/Images" directory.

I tested by doing the following in the didSavePhoto delegate method:

    func cameraDidSavePhoto(atPath assetURL: URL!) {
        print("cameraDidSavePhotoAtPath: \(assetURL)")
        guard let assetURL = assetURL?.path else { return }
        // Use fileURLWithPath to get a valid url that can be used to load image
        let url = URL(fileURLWithPath: assetURL)
        guard let data = try? Data(contentsOf: url) else { return }
        let image = UIImage(data: data)
        print(image)
    }

In my case, I always got returned from guard let data = try? Data(contentsOf: url) else { return }. This is very likely caused by the fact that the image is not saved to disk.

Thank you!

lichenglu avatar Oct 22 '16 21:10 lichenglu

Experienced the same issue. Fix is here: https://github.com/tdginternet/TGCameraViewController/pull/88

neilabdev avatar Feb 12 '17 21:02 neilabdev