Get Time Stamp/Location of iOS Video - ios

I can get the .MOV file with the Image Picker, but how can I find the location it was taken at and the time it was taken?
My image picker:
- (void) imagePickerController: (UIImagePickerController *) picker
didFinishPickingMediaWithInfo: (NSDictionary *) info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:NO completion:nil];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0)
== kCFCompareEqualTo) {
NSString *movieFile = [[info objectForKey:
UIImagePickerControllerMediaURL] path];
NSURL *movieURL = [NSURL fileURLWithPath:movieFile];
NSLog(#"Movie File %#",movieURL);
[self dismissViewControllerAnimated:YES completion:nil];
}
}

I don't know about the location it was taken but you can use File Attribute Keys to get the NSFileCreationDate using attributesOfItemAtPath:error:. There is a convenience method on NSDictionary fileCreationDate
NSError *error;
NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:movieFile error:&error];
if (!attributes) {
// Use the error
}
NSDate *createdDate = [attributes fileCreationDate];
If you want to access the meta-data on the MOV file you can have a look at the EXIF data, I don't know if there is a iOS library for this though.

Related

Image Metadata from Action Extension

Usually when i want to read some image metadata in iOS i use imagePickerController to choose the image and Photos Framework and imagePickerController:didFinishPickingMediaWithInfo:
to get image info and extract the metadata like this
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImagePickerControllerSourceType pickerType = picker.sourceType;
if(pickerType == UIImagePickerControllerSourceTypePhotoLibrary)
{
NSURL *url = [info objectForKey:UIImagePickerControllerReferenceURL];
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:#[url,] options:nil];
PHAsset *asset = fetchResult.firstObject;
[self metaDataFromPhotoLibrary:asset];
[self dismissViewControllerAnimated:YES completion:NULL];
}
}
-(void)metaDataFromPhotoLibrary:(PHAsset*)asset{
// NSLog(#"Start metadata");
PHContentEditingInputRequestOptions *options = [[PHContentEditingInputRequestOptions alloc] init];
options.networkAccessAllowed = YES; //download asset metadata from iCloud if needed
[asset requestContentEditingInputWithOptions:options completionHandler:^(PHContentEditingInput *contentEditingInput, NSDictionary *info) {
CIImage *fullImage = [CIImage imageWithContentsOfURL:contentEditingInput.fullSizeImageURL];
NSDictionary *metadata = fullImage.properties;
NSMutableDictionary *imageMetadata = nil;
imageMetadata = [[NSMutableDictionary alloc] initWithDictionary:metadata];
NSString *dateString = metadata [#"{TIFF}"] [#"DateTime"];
NSString *latitude = metadata [#"{GPS}"][#"Latitude"];
NSString *longitude = metadata [#"{GPS}"][#"Longitude"];
//etc etc etc
}
But can't do the same thing from an Action Extension
in the extension code i use a code like this to get the selected image
- (void)viewDidLoad {
[super viewDidLoad];
// Get the item[s] we're handling from the extension context.
// For example, look for an image and place it into an image view.
// Replace this with something appropriate for the type[s] your extension supports.
BOOL imageFound = NO;
for (NSExtensionItem *item in self.extensionContext.inputItems) {
for (NSItemProvider *itemProvider in item.attachments) {
if ([itemProvider hasItemConformingToTypeIdentifier:(NSString *)kUTTypeImage]) {
// This is an image. We'll load it, then place it in our image view.
__weak UIImageView *immagine = self.immagine;
[itemProvider loadItemForTypeIdentifier:(NSString *)kUTTypeImage options:nil completionHandler:^(UIImage *image, NSError *error) {
if(image) {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
[immagine setImage:image];
}];
}
}];
imageFound = YES;
break;
}
}
if (imageFound) {
// We only handle one image, so stop looking for more.
break;
}
}
}
Using UIImagePNGRepresentation or UIImageJPEGRepresentation i lost many metadata and i can read only a few data !
How can i get all image metadata from the image selected from action Extension ?
Thank you so mush
Note: i 've found some app in the AppsStore that read all metadata dictionary from extension , so there must be a solution for my problem !
Thank you again
Vanni

How to Save UIImage to a Transparent PNG?

I'm not sure where I'm doing the mistake, that's why I'd like to paste here some block of codes in order to find out what went wrong.
I have some PNG images that have transparent background and with using the block of codes below I'm trying save the selected image to application folder and NSUserDefault. And then I'm trying to call that saved image from application folder and display on UIBarButtonItem.
The purpose of saving to NSUserDefault as well is for testing.
So,
This is how I'm selecting the image.
- (IBAction)btnPressed:(UIButton *)sender
{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeSavedPhotosAlbum])
{
UIImagePickerController *imgPicker = [[UIImagePickerController alloc] init];
imgPicker.delegate = self;
imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imgPicker.mediaTypes = [NSArray arrayWithObject:(NSString *)kUTTypeImage];
imgPicker.allowsEditing = NO;
[self presentViewController:imgPicker animated:YES completion:nil];
}
}
And with this I'm saving it to application using didFinishPickingMediaWithInfo;
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *) kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
//I believe this has the problem.
NSData *pngData = UIImagePNGRepresentation(image);
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"AppImages"];;
NSError *error = nil;
if (![[NSFileManager defaultManager] fileExistsAtPath:stringPath])
[[NSFileManager defaultManager] createDirectoryAtPath:stringPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *fileName = [stringPath stringByAppendingPathComponent:#"logoImage.png"];
[pngData writeToFile:fileName atomically:YES];
NSUserDefaults *logoUserDef = [NSUserDefaults standardUserDefaults];
[logoUserDef setValue:pngData forKey:#"logoImageData"];
}
}
Note: I don't have problem for the images that doesn't have transparent background.
And lastly this is how I'm calling it from both Application and NSUserDefault.
Using both NSUserDefaults and AppDirectory to see if they're showing different result.
if ([[NSUserDefaults standardUserDefaults] objectForKey:#"logoImageData"])
{
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
UIImage *testing = [UIImage imageWithData:[user valueForKey:#"logoImageData"]];
NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage * image = [self loadImageWithFileName:#"logoImage" ofType:#"png" inDirectory:documentsDirectory];
UIBarButtonItem *logoBarButton = [[UIBarButtonItem alloc] initWithImage:image style:UIBarButtonItemStylePlain target:self action:#selector(logoTapped)];
logoBarButton.imageInsets = UIEdgeInsetsMake(9, 0, 9, 90);
logoBarButton.image = [logoBarButton.image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[self.navigationItem setLeftBarButtonItems:#[logoBarButton]];
}
And method that returns UIImage from App Document.
-(UIImage *)loadImageWithFileName:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:#"%#/%#/%#.%#", directoryPath, #"AppImages", fileName, [extension lowercaseString]]];
return result;
}
I don't know from where I'm doing wrong but as always, when I select image that has transparent background. It always convert its background to White. I need to save it as it is as transparent. I've searched a lot but couldn't find anything to solve this.
Any help is appreciated.
Solved the problem!
The block of codes that I posted with my question have nothing wrong. The problem was occurring from iTunes, when I synchronize images from my Mac to my iPhone iTunes convert them to JPEG and because of this images loses their transparency etc... and when I select images using my app, I was selecting them as JPEG without being aware and that's why I had these problems.
So I came up with a solution to have images via email. I just send them from my mac to iPhone with e-mail, and by this images didn't lose anything as a feature. After that when I used them in app as a right image, the program worked as it supposed to be.
You wrote
[logoUserDef setValue:pngData forKey:#"logoImageData"];
but I don't see where you synchronize. So add:
[logoUserDef synchronize];
I was able to preserve the transparency when saving an image to the Documents directory after using
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[self dismissViewControllerAnimated:YES completion:nil];
UIImage *img = [info valueForKey:UIImagePickerControllerOriginalImage];
[yourButton setImage:img forState:UIControlStateNormal];
NSData *pngData = UIImagePNGRepresentation(img);
//do something with pngData
}
The key is the UIImagePickerControllerOriginalImage instead of other options like UIImagePickerControllerEditedImage.

UIImagePickerController doesn't delete temp video capture file from /tmp/capture

My app is recording videos using a UIImagePickerController and saves it to the camera roll just fine but my problem is that the recorded video is also being saved in the /tmp/capture directory and doesn't get deleted. My app slowly accumulates a long list of videos stored in the tmp/capture directory using lots of unnecessary data because of this. I am using ARC and iOS7 SDK.
-(BOOL)startCameraControllerFromViewController:(UIViewController*)controller usingDelegate:(id )delegate forType: (int)type
{
if (([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO)
|| (delegate == nil)
|| (controller == nil)) {
return NO;
}
UIImagePickerController *cameraUI = [[UIImagePickerController alloc] init];
cameraUI.sourceType = UIImagePickerControllerSourceTypeCamera;
if(type == IMAGE )
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
if(type == VIDEO)
{
cameraUI.videoMaximumDuration = 60.0f;
cameraUI.videoQuality = UIImagePickerControllerQualityType640x480;
cameraUI.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
}
cameraUI.allowsEditing = NO;
cameraUI.delegate = delegate;
[controller presentViewController: cameraUI animated: YES completion:nil];
return YES;
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
if(CFStringCompare ((__bridge_retained CFStringRef)mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)//video
{
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath))
UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,#selector(video:didFinishSavingWithError:contextInfo:), nil);
[self dismissViewControllerAnimated:NO completion:nil];
globalDel.videoPath = moviePath;
globalDel.videoToUpload = [NSData dataWithContentsOfFile:moviePath];
}
}
globalDel.videoPath is defined as: #property(nonatomic,retain)NSString *videoPath;
globalDel.videoToUpload is defined as: #property(nonatomic,retain)NSData *videoToUpload;
I set them to NULL once done with them.
I use a delegate file to keep those references for use between different navigation controllers.
Why is the app saving the file to the tmp folder each time and how do I stop it from doing that?
Thanks
You need to delete the directory containing the movie once your upload is finished (which will also delete the temp file):
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:globalDel.videoPath]) {
NSError *error;
// Attempt to delete the folder containing globalDel.videoPath
if ([fileManager removeItemAtPath:[globalDel.videoPath stringByDeletingLastPathComponent] error:&error] != YES) {
NSLog(#"Unable to delete file: %#", [error localizedDescription]);
}
}

Saving picture from app

I am trying to make my application open the camera app to take a save pictures.
from my application i am launching the camera application to take a picture with the following code:
-(IBAction)TakePhoto:(id)sender {
picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
[picker setSourceType:UIImagePickerControllerSourceTypeCamera];
[self presentViewController:picker animated:YES completion:NULL];
[picker release];
//save image??:
//UIImageWriteToSavedPhotosAlbum(UIImage *image, id completionTarget, SEL completionSelector, void *contextInfo);
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
[self dismissViewControllerAnimated:YES completion:NULL];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[self dismissViewControllerAnimated:YES completion:NULL];
}
My problem is that once the button is pressed, the camera comes out and allows me to take a picture. Once the picture is take, the image is shown and i have the option to "retake" or "use". My issue is that if i click "use" the image is not saved to the camera roll. Is there a possibility to save the image and eventually change the "use" button to say "save"?
Thank you for you help!
The photo isn't being saved because you never actually added the code to save the image to the didFinishPickingMediaWithInfo: delegate method. All you have to do is add the line that you commented out in TakePhoto: to this function and you will be able to save the photo to the camera roll. E.x:
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
image = [info objectForKey:UIImagePickerControllerOriginalImage];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, NULL);
[self dismissViewControllerAnimated:YES completion:NULL];
}
static NSDateFormatter* dateFormatter;
- (NSString*) generateNameWithExtension: (NSString*) extension
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = #"yyyyMMddHHmmssSSS";
});
NSDate* now = [NSDate date];
NSString* string = [dateFormatter stringFromDate:now];
string = [NSString stringWithFormat:#"%#.%#",string,extension];
return string;
}
- (NSString*) saveImage: (UIImage*) image WithExtension: (NSString*) extension
{
extension = [extension lowercaseString];
NSData* data;
if ([extension isEqualToString:#"png"])
{
data = UIImagePNGRepresentation(image);
}else if ([extension isEqualToString:#"jpg"]||[extension isEqualToString:#"jpeg"])
{
data = UIImageJPEGRepresentation(image, 1.0);
}else{
NSLog(#"Error save local image, Extension: (%#) is not recognized, use (PNG/JPG)",extension);
return nil;
}
NSString* imageName = [self generateNameWithExtension:extension];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString *finalPath = [documents stringByAppendingPathComponent:imageName];
[data writeToFile:finalPath options:NSAtomicWrite error:nil];
return finalPath;
}
This code save image in folder you app. You can later use it: [UIImage imageWithContentsOfFile:finalPath]

How to know the URL of a saved Photo on my Photo Library?

I'm working on a simple puzzle game that use an image as background of the board game.
Everything works fine, but there are a little thing bothering me. When I take a photo from camera I can save it on my Photo Library but I don't know where it was saved, I mean, what is the exactly URL of my photo.
Here is the code where I save photos:
- (void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
CGRect bounds;
bounds.origin = CGPointZero;
bounds.size = imageToSave.size;
[scrollView setContentSize:bounds.size];
[scrollView setContentOffset:bounds.origin];
[imageView setFrame:bounds];
imageView.image = imageToSave;
if ([info objectForKey:#"UIImagePickerControllerReferenceURL"] == nil) {
// Save the new image (original or edited) to the Camera Roll
ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
ALAssetOrientation orientation; //= [[[info objectForKey:#"UIImagePickerControllerMediaMetadata"] objectForKey:#"Orientation"] integerValue];
NSString *infoOrientation = [[info objectForKey:#"UIImagePickerControllerMediaMetadata"] objectForKey:#"Orientation"];
switch ([infoOrientation integerValue]) {
case 3:
orientation = ALAssetOrientationUp;
break;
case 6:
orientation = ALAssetOrientationRight;
break;
default:
orientation = ALAssetOrientationDown;
break;
}
[al writeImageToSavedPhotosAlbum:[imageToSave CGImage] orientation:orientation completionBlock:^(NSURL *assetURL, NSError *error) {
if (error == nil) {
NSLog(#"saved");
savedImageCam = assetURL;
} else {
NSLog(#"error");
}
}];
} else {
NSLog(#"URL from saved image: %#", savedImageCam);
NSLog(#"URL from photo image: %#", [info objectForKey:#"UIImagePickerControllerReferenceURL"]);
}
}
// Handle a movie capture
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = (NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
}
}
[self dismissViewControllerAnimated:YES completion:nil];
}
The call method UIImageWriteToSavedPhotosAlbum doesn't return the URL, and the instance variable info just bring the URL if I call the Photo Library directly not through of camera.
Anybody knows how can I fix it?
Don't use UIImageWriteToSavedPhotosAlbum. Instead, use the ALAssetsLibrary and writeImageToSavedPhotosAlbum:orientation:completionBlock:. The completionBlock then gives you access to the asset URL that you can use to get the image again in the future.

Resources