Search code examples
iosiphonescreenshot

Check if an uploaded image is a screenshot iOS


Via UIImagePickerController, is there a way to tell if the image the user chose from their library is a screenshot? Also, is there a timestamp associated with screenshots? I've read Get Exif data from UIImage - UIImagePickerController, but do screenshots in particular have this data?


Solution

  • Yes YOU CAN. I'm doing it.

    First of all, clarify that I am working with NativeScript in Javascript language, but the code is easily understandable for any iOS developer.

    First you need to get the PHAsset from the result of your UIImagePickerController, you can easily do it based on answers like this: How to retrieve PHAsset from UIImagePickerController

    Then you must obtain its Exif data, check the UserComment property, and if it corresponds to Screenshot, it will effectively be a screenshot.

    PHCachingImageManager.defaultManager().requestImageDataForAssetOptionsResultHandler( asset, this.requestImageDataForAssetOption, (data,uti,orientation,info) => {
        if( !data ) return resolve(false);
        
        let options = NSDictionary.dictionaryWithObjectForKey(kCFBooleanFalse,'kCGImageSourceShouldCache')
        let imgSrc = CGImageSourceCreateWithData(data, options)
        let metadata = CGImageSourceCopyPropertiesAtIndex(imgSrc, 0, options)
        var Exif = metadata.objectForKey('{Exif}')
        if( !Exif ) return resolve(false);
    
        let userComment = String( Exif.valueForKey('UserComment') )
    
        if( userComment == 'Screenshot' ) return resolve(true);
        resolve(false)
    })
    

    You can also easily get the date the screenshot was taken from the PHAsset con su propiedad creationDate.