将位置元数据添加到照片库中保存的UIImage

时间:2017-07-22 18:24:52

标签: ios swift uiimage cllocationmanager

我正在尝试将位置元数据(long,lat)附加到使用相机拍摄的图像的EXIF元数据中。但是,当我尝试显示图像的位置时,无法找到它。为了调试这个,我一直在打印EXIF数据到控制台,但位置数据不存在。有人可以找到我出错的地方吗?

// SAVE IMAGE TO PHOTO LIBRARY
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

    let image = info[UIImagePickerControllerOriginalImage] as! UIImage

    func addAsset(image: UIImage, location: CLLocation? = nil) {
        PHPhotoLibrary.shared().performChanges({
            // Request creating an asset from the image.
            let creationRequest = PHAssetChangeRequest.creationRequestForAsset(from: image)
            // Set metadata location
            if let location = location {
                creationRequest.location = location
            }
        }, completionHandler: { success, error in
            if !success { NSLog("error creating asset: \(String(describing: error))") }
        })
    }

    addAsset(image: image, location: manager.location)
    UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil)

    // THIS WORKS WITH THE CORRECT LOCATION
    print(manager.location!)

    // THIS PRINTS TO CONSOLE BUT IS MISSING ANY LOCATION DATA
    print(info[UIImagePickerControllerMediaMetadata]!)


    var chosenImage:UIImage?
    var location = "Not known"
    var timeTaken = "Not known"

   /////////////////////////
   // This is only working when I select an image from the photo library, rather than straight from the camera which is my intended behaviour.

    if let URL = info[UIImagePickerControllerReferenceURL] as? URL {
        print("We got the URL as \(URL)")
        let opts = PHFetchOptions()
        opts.fetchLimit = 1
        let assets = PHAsset.fetchAssets(withALAssetURLs: [URL], options: opts)
        for assetIndex in 0..<assets.count {
            let asset = assets[assetIndex]
            location = String(describing: asset.location)
            timeTaken = asset.creationDate!.description

            CLGeocoder().reverseGeocodeLocation(asset.location!, completionHandler: {(placemarks, error) -> Void in
                print(location)

                if let error = error {
                    print("Reverse geocoder failed with error" + error.localizedDescription)
                    return
                }

                if placemarks != nil && placemarks!.count > 0 {
                    DispatchQueue.main.async {
                        self.geoLabel.text = placemarks![0].locality!
                    }
                } else {
                    print("There's a problem with the data, motherfucker")
                }
            })
        }
    }

    if let editedImage = info[UIImagePickerControllerEditedImage] as? UIImage {
        chosenImage = editedImage
    } else if let selectedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
        chosenImage = selectedImage
    }

    dismiss(animated: true) {
        DispatchQueue.main.async {
            self.myImageView.image = chosenImage
            self.locationLabel.text = location
            self.timeLabel.text = timeTaken
        }
    }
}

EXIF打印

{
    DPIHeight = 72;
    DPIWidth = 72;
    Orientation = 6;
    "{Exif}" =     {
        ApertureValue = "2.27500704749987";
        BrightnessValue = "1.5026944451448";
        ColorSpace = 1;
        DateTimeDigitized = "2017:07:22 7:16:12 pm";
        DateTimeOriginal = "2017:07:22 7:16:12 pm";
        ExposureBiasValue = 0;
        ExposureMode = 0;
        ExposureProgram = 2;
        ExposureTime = "0.0303030303030303";
        FNumber = "2.2";
        Flash = 24;
        FocalLenIn35mmFilm = 29;
        FocalLength = "4.15";
        ISOSpeedRatings =         (
            320
        );
        LensMake = Apple;
        LensModel = "iPhone 6 back camera 4.15mm f/2.2";
        LensSpecification =         (
            "4.15",
            "4.15",
            "2.2",
            "2.2"
        );
        MeteringMode = 5;
        PixelXDimension = 3264;
        PixelYDimension = 2448;
        SceneType = 1;
        SensingMethod = 2;
        ShutterSpeedValue = "5.060000179460458";
        SubjectArea =         (
            1631,
            1223,
            1795,
            1077
        );
        SubsecTimeDigitized = 551;
        SubsecTimeOriginal = 551;
        WhiteBalance = 0;
    };
    "{MakerApple}" =     {
        1 = 5;
        14 = 0;
        2 = <a3009400 f300d200 02010701 03019000 4800b200 ad00a500 9e009900 95008900 96008900 f000d200 fe001001 fb008100 4800ae00 ac00b000 aa00a500 9f009900 a7007a00 db00ea00 f4000201 d5007f00 5a00ae00 b800b500 b000aa00 9f009700 a6005b00 d800fc00 d7000b01 df007400 8100bf00 bb00b900 b200a600 a7000501 c700a500 d400fe00 f900fd00 cc005c00 ad00c400 c000b200 a900e900 59016901 c400ed00 f800ef00 e300ff00 c8007a00 c000c200 b400d100 4b017701 5f016201 ba00dd00 f400e600 07011101 b7008300 c100c200 f4007e01 79016401 37015301 b400eb00 0b01d000 fa00f000 aa008f00 c100bf00 14016801 69015f01 36014b01 ae00e100 eb00e100 0601fb00 a7009e00 c100bf00 fe005f01 5e016701 38014101 ae00d800 eb00dc00 ee00e400 7400af00 be00bf00 f6005401 52015b01 3a013c01 b100bd00 e200fe00 0501e500 7f00b600 b900bb00 e9005601 44013b01 39012801 b900b100 d400e700 fc00cd00 7f00b700 ba00b300 d0005801 31012a01 27011401 bd00b100 ce00fa00 ed00b200 7e00de00 bd00b000 a8002b01 18011001 0d010301 bd00b200 b800e700 f3009b00 81002c01 50010501 ac00a700 1b012501 2a011f01 b900ac00 a300ed00 d2007e00 a700f700 28015b01 36011001 4f015501 44014d01 c000ba00 b000c800 d2007700 b100e400 a600f100 50015a01 45015101 3a014401>;
        20 = 4;
        3 =         {
            epoch = 0;
            flags = 1;
            timescale = 1000000000;
            value = 61388784923625;
        };
        4 = 1;
        5 = 226;
        6 = 221;
        7 = 1;
        8 =         (
            "0.08794327825307846",
            "-0.8311901688575745",
            "-0.5451160669326782"
        );
        9 = 275;
    };
    "{TIFF}" =     {
        DateTime = "2017:07:22 7:16:12 pm";
        Make = Apple;
        Model = "iPhone 6";
        ResolutionUnit = 2;
        Software = "10.3.2";
        XResolution = 72;
        YResolution = 72;
    };
}

0 个答案:

没有答案