Determine whether the selected video is .MOV or .MP4 in iOS - ios

In my application, i need to select mp4 video file from iPhone Gallery and upload it on server.Is it possible to check whether the selected file is .mp4 or .mov?

I got the file extension from the below snippet
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeMovie])
{
NSString * videoURL=info[UIImagePickerControllerReferenceURL];
NSString *lastPath = [videoURL lastPathComponent];
NSString *fileExtension = [lastPath pathExtension];
NSLog(#"File extension %#",fileExtension);
}
}

No we cant read it there by selecting.once you selected it, that file will came in to your app there you decode to NSdata and encode it back.in between this process there is a chance to find out.try this method, Hope this will work.

Related

Xcode/ios: How do you get name of image after letting user select one and displaying it?

I have code working that lets user select an existing image and have it displaying nicely in UIView on screen.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
self.imageView.image = chosenImage;
NSLog(#"image is%#",chosenImage);
[picker dismissViewControllerAnimated:YES completion:NULL];
}
How do I get the name of the file to persist it for future reference? chosenImage just shows you a bunch of metadata about the file.
Thank you.
As you see below, you can't get the name of the selected image from UIImagePickerControllerDelegate using the currently existing API.
Here are UIImagePickerControllerDelegate's available Editing Information Keys:
NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;
However, you may want to make use of AssetsLibrary.framework to obtain the name of an image managed by Photos application:
#import AssetsLibrary;
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library assetForURL:info[UIImagePickerControllerReferenceURL]
resultBlock:^(ALAsset *fileAsset) {
NSLog(#"%#", [[fileAsset defaultRepresentation] filename]);
} failureBlock:nil];
}
try NSLog(#"%#",info) at the beging of that method and see if the name is in the dictionary.

didFinishPickingMediaWithInfo get Image filename

I am trying to get the image filename from the camera. I have seen many, many posts about retrieving the filename using UIImagePickerControllerReferenceURL or UIImagePickerControllerMediaURL, but that only gets you a filename similar to: //asset/asset.JPG?id=D1D715A8-6956-49FF-AF07-CE60FE93AE32&ext=JPG. I believe this is because the image is not saved yet, thus a name like img0001.jpg in not available.
Does anyone know how to get the filename? I am as suing I need to do this after UIImageWriteToSavedPhotosAlbum, but I cannot seem to figure it out.
I have seen the question asked many times, but none seem to 'really' answer it, they just always give the filename from an image chosen from the picker not from the camera.
Thanks for any help
** UPDATE:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self.popoverController dismissPopoverAnimated:true];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
[self dismissViewControllerAnimated:YES completion:nil];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
imageView.image = image;
if (newMedia)
{
UIImageWriteToSavedPhotosAlbum(image,
self,
#selector(image:finishedSavingWithError:contextInfo:),
nil);
NSURL *refURL = [info valueForKey:UIImagePickerControllerMediaURL];
// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSLog(#"[imageRep filename] : %#", [imageRep filename]);
};
// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
}
}
}
My code above, but when imageRep filename is logged it is null.

Record video on iOS and attach to email

I am trying to develop an app that can record video then attach it to email.
Here's what I did, but it isn't working.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
[self dismissModalViewControllerAnimated:NO];
// Handle a movie capture
if (CFStringCompare ((__bridge_retained CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(moviePath)) {
UISaveVideoAtPathToSavedPhotosAlbum(moviePath, self,
#selector(video:didFinishSavingWithError:contextInfo:), nil);
videoURL = [[NSURL URLWithString:moviePath] init];
}
}
}
For Attachment:
[tempMailCompose addAttachmentData:[NSData dataWithContentsOfURL:videoURL] mimeType:#"video/MOV" fileName:#"defectVideo.MOV"];
The video recording and saving it to Photo Library is working good, my problem is the attachment.
What could be wrong?
For attachment you must use NSData. in case video attachment you have to use MimeType #"video/quicktime".
For more clarification please refer apple documentation.
MFMailComposeViewController
For attachment in mail use the following code :
[tempMailCompose addAttachmentData:[NSData dataWithContentsOfURL:videoURL] mimeType:#"video/quicktime" fileName:#"defectVideo.MOV"];
and rest of part is ok, i think.
I figured it out, here:
I replaced this...
videoURL = [[NSURL URLWithString:moviePath] init];
With this...
videoURL = [[NSURL alloc] initFileURLWithPath:moviePath];
It's working now.

Open UIImage from photo library with data

I have two view controllers, in one I pick the image from the photo library and assign it to an image property. With this picker:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo
But instead I would like to just get the address/data of the image from the picker in the first controller, push the next view controller and then open the image with that address there. So then I do the following:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
When I print info of the photo it looks something like this:
assets-library://asset/asset.JPG?id=52219875-C221-4F60-B9D4-984AAADB6E63&ext=JPG
The problem is that UIImage has methods initWithData or imageWithData but these accept NSData and not NSDictionary.
How can I open an image from a photo library using its data?
This method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)img
editingInfo:(NSDictionary *)editInfo
shouldn't be used, it's been deprecated since ios3
This method:
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info
returns a dictionary, as you report.
The image pointer is one of the objects in this dictionary. You obtain it as such:
UIImage* image = [info objectForKey:UIImagePickerControllerOriginalImage];
Here's Apple's documentation for that method.
Basically there are a few keys in that dictionary, listen here:
NSString *const UIImagePickerControllerMediaType;
NSString *const UIImagePickerControllerOriginalImage;
NSString *const UIImagePickerControllerEditedImage;
NSString *const UIImagePickerControllerCropRect;
NSString *const UIImagePickerControllerMediaURL;
NSString *const UIImagePickerControllerReferenceURL;
NSString *const UIImagePickerControllerMediaMetadata;
You probably want UIImagePickerControllerEditedImage. So this is what you're looking for:
UIImage *theImage = [info valueForKey:UIImagePickerControllerEditedImage];
Of course you can get other info from the image with the other keys in the dictionary too.
The dictionary provides the URL for the image.
So load the data by
NSData *imageData = [NSData dataWithContentsOfURL:[info valueForKey: UIImagePickerControllerReferenceURL]];
and use this data for the NSImage initWithData method.

How can I keep track of media created/chosen by UIImagePickerController?

I'm building an iOS app that allows the user to upload videos from UIImagePickerController, either by recording or choosing them from the Camera Roll, as well as also play the chosen video. My question is, how would I go about keeping a reference to the videos that have been chosen this way? I want to do this so that if the video is still present on the device, I can use the local file rather than streaming the uploaded file.
When
imagePickerController:didFinishPickingMediaWithInfo:
returns, the URL in:
[info objectForKey:UIImagePickerControllerMediaURL];
Is in the format of: "file://localhost/private/var/mobile/Applications/ /tmp//trim.z2vLjx.MOV"
I'm lead to believe that the "/tmp/" directory is temporary, and therefore not suitable to save the URL for that location.
I can get all of the videos on the device through ALAssetsLibrary, but because I don't have a way of distinguishing them, this doesn't help me. I've been attempting to use:
[result valueForProperty:ALAssetPropertyDate];
To distinguish the videos, but I need a way of getting the creation date from UIImagePickerController for this to be useful.
I've finally managed to find a solution:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if(CFStringCompare((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo)
{
//Dismiss the media picker view
[picker dismissModalViewControllerAnimated:YES];
//Get the URL of the chosen content, then get the data from that URL
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *webData = [NSData dataWithContentsOfURL:videoURL];
//Gets the path for the URL, to allow it to be saved to the camera roll
NSString *moviePath = [[info objectForKey:UIImagePickerControllerMediaURL] path];
if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath))
{
ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
//The key UIImagePickerControllerReferenceURL allows you to get an ALAsset, which then allows you to get metadata (such as the date the media was created)
[lib assetForURL:[info objectForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
NSLog(#"created: %#", [asset valueForProperty:ALAssetPropertyDate]);
} failureBlock:^(NSError *error) {
NSLog(#"error: %#", error);
}];
}
}
As per usual, the solution was found by reading the documentation a little more thoroughly. Hopefully this'll help someone else out at some point.
You can easily keep a record of the videos you have on the device. Either by keeping a data base (which I think would be too much) or just a file with a list of your videos. In that list, you could have the URL of the assets.

Resources