Search code examples
swiftlocationphotosframeworkphasset

Photos Framework - How to obtain CLLocation of image from PHAsset?


I am able to fetch image successfully from PHFetchResults and load it into UIImage nicely but when I try to obtain image's location, I get error:

  • "fatal error: unexpectedly found nil while unwrapping an Optional value" at NSLog statement during runtime.
var imageAsset:PHAsset = self.photosAssetFetchResult[self.index] as PHAsset
NSLog("Location is %@", imageAsset.location)

If I replace above NSLog statement with following statement to get Description(or any other Properties like Length, Pixel Dimension etc NSLog returns it correctly) it does not throw error.

NSLog("Description is %@", imageAsset.Description)

I tried using PHImageManger.requestImageDataForAsset etc. I get many other properties as below but Location.

[{Exif}: {
    ColorSpace = 1;
    ComponentsConfiguration =     (
        1,
        2,
        3,
        0
    );
    ExifVersion =     (
        2,
        2,
        1
    );
    FlashPixVersion =     (
        1,
        0
    );
    PixelXDimension = 1280;
    PixelYDimension = 960;
    SceneCaptureType = 0;
}, ColorModel: RGB, DPIWidth: 72, Depth: 8, Orientation: 1, DPIHeight: 72, PixelHeight: 960, PixelWidth: 1280, {TIFF}: {
    Orientation = 1;
    ResolutionUnit = 2;
    XResolution = 72;
    YResolution = 72;
}]

Please advise.


Solution

  • It was a genuine error. The assets i was using did not have location. Exactly same code works just fine with pictures taken by iPhone 4 and iPhone 6.