didFinishPickingMediaWithInfo duplicate file when saving - ios

I have a simple App than you have the possibility to choose an existent video from photo library or you can take a video with UIImagePickerController.
I added the following code for when I make a new video with camera I can save it in photo Gallery if I need in the future.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the selected Video.
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
// Convert to Video data.
NSData *imageData = [NSData dataWithContentsOfURL:videoURL];
// Save Video to Photo Album
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
NSURL *recordedVideoURL= [info objectForKey:UIImagePickerControllerMediaURL];
if ([library videoAtPathIsCompatibleWithSavedPhotosAlbum:recordedVideoURL]) {
[library writeVideoAtPathToSavedPhotosAlbum:recordedVideoURL
completionBlock:^(NSURL *assetURL, NSError *error){}
];
}
[library release];
[picker dismissModalViewControllerAnimated:NO];
}
Now my issue is when I select a video from Photo Library this same video is duplicated as this code save always a new video.
Is it a way to detect if you selected the video from Photo Library ?

Before saving the video, check the imagePickerController's sourceType. Only save the video if sourceType is UIImagePickerControllerSourceTypeCamera.
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
// Save the video
}

Related

How can i retrieve an image filename using UIImagePickerController?

I would like to know how to retrieve the filename of an image taken with the camera using UIImagePickerController.
I don't need to do anything with it, i just have to display the filename, like something you would see when you attach a file when you're sending an email.
So far, i'm using AssetsLibrary :
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// 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]);
self.photoNameLabel.text = [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];
self.photoLoaded = info[UIImagePickerControllerEditedImage];
[self viewWillLayoutSubviews];
}
But imageAsset will always be nil if it comes from the device camera, and if not, the image is named IMG_00XX.jpg, even if i downloaded it from the internet before, with a different name.
Can i get the name of a picture in an ios environment? If so, how am i supposed to do that?
There is no name when you take a photo with the camera. All there is is the in-memory UIImage instance. It's not associated with a file.
If you wish to add this camera image to an email as an attachment, give it whatever name you want such as "photo.jpg" or "picture.jpg" or anything else you wish to use.

memory increase use UIImagePickerController to take photo and save to user's album

I'm using MWPhotoBrowser to display my photos taken by UIImagePickerController.
Here is my code for saving to user's album.
-(void)imagePickerController:(UIImagePickerController *)pickerdidFinishPickingMediaWithInfo:(NSDictionary *)info
{
// NSDictionary* metadata=info[UIImagePickerControllerMediaMetadata];
// [[PHPhotoLibrary sharedPhotoLibrary]performChanges:^{
// NSURL* url=info[UIImagePickerControllerReferenceURL];
// PHAssetChangeRequest* assetChangeReques= [PHAssetChangeRequest creationRequestForAssetFromImageAtFileURL:url];
// PHAssetCollectionChangeRequest* request=[PHAssetCollectionChangeRequest changeRequestForAssetCollection:self.assetCollection];
// [request addAssets:#[[assetChangeReques placeholderForCreatedAsset]]];
// }completionHandler:^(BOOL success,NSError* error){
// if (success) {
// // self.previewImgView.image=image;
// }
// }];
ALAssetsLibrary* alassetsLibary=[[ALAssetsLibrary alloc] init];
[alassetsLibary saveImageData:UIImageJPEGRepresentation(info[UIImagePickerControllerOriginalImage], 1.0f) toAlbum:self.assetCollection.localizedTitle metadata:info[UIImagePickerControllerMediaMetadata] completion:^(NSURL* assetUrl,NSError* error)
{
}failure:^(NSError* error){
}];
self.previewImgView.image=[info[UIImagePickerControllerOriginalImage] getThumbnailWithSize:self.previewImgView.bounds] ;
alassetsLibary=nil;
info=nil;
}
I have used to ways to save the photo above. But it can't handle the problem same as yours.
In the second method. I have tried to change the UIImageJPEGRepresentation parameter from 0.1 to 1.0. But it did't work.
I have try to let alassetsLibary=nil; info=nil; But when I open the photoBrowser. The memory increase so fast.
If anybody has solution,please tell me!!
And I wondering how Apple handle with this issue in native camera app as well.
Gratitude.
I always use this to save pics to the album:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:((UIImage *)[info objectForKey:UIImagePickerControllerEditedImage]).CGImage
metadata:[info objectForKey:UIImagePickerControllerMediaMetadata]
completionBlock:^(NSURL *assetURL, NSError *error) {
NSLog(#"assetURL %#", assetURL);
imageURL=[assetURL absoluteString];
//save URL for future use
[[NSUserDefaults standardUserDefaults] setObject:imageURL forKey:#"imageurl"];
}];
[picker dismissViewControllerAnimated:YES completion:NULL];
}

Save a photo to the camera roll and make sure it actually saved

I am currently saving a UIImage to the camera roll this way.
UIImageWriteToSavedPhotosAlbum(finalPicture.image, nil, nil, nil);
But what happens if the user denies us permission to access their photos... how can I tell that this has happened and display an error message?
To save the image to the camera roll I'm Using ALAssetsLibrary so in the method:
//Called after taking the photo with the camera or selected the image from the gallery
- (void)imagePickerController:(UIImagePickerController *) Picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *referenceURL;
if(Picker.sourceType==UIImagePickerControllerSourceTypeCamera){
UIImage* takenImage=info[UIImagePickerControllerOriginalImage];
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Request to save the image to camera roll
[library writeImageToSavedPhotosAlbum:[takenImage CGImage] orientation:(ALAssetOrientation)[takenImage imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
//NOT SAVED
//DISPLAY ERROR THE PICTURE CAN'T BE SAVED
} else {
//SAVED
}
}];
}
}else{
//Selected from gallery
}
Also remember that you have to check first if the camera is available.
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
//The camera is available
}else{
//No camera available
}

Giving a video file a specific name before saving to photo roll - iOS

I am working on an iOS app and I recently implemented a imagePicker in a popover that allows a user to record photos and videos. I would like to store these in the iPad's photo roll so that the user can sync them in iTunes later. I've been able to do this successfully, but I would like to give each photo and video a unique name that contains useful information about the photo and video so I can load them later. Specifically, I would like to store the photo using a property of the class, live_trial_id, as the filename. Below is the code that I am currently using to store my photos and videos to the photo roll. I understand that I could do this with metadata for pictures, but for the videos I am lost. Thanks in advance for any help with this issue!
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToSave;
// Handle a still image capture
if( [mediaType isEqualToString:#"public.image"]){
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToSave = editedImage;
} else {
imageToSave = originalImage;
}
// Get the image metadata
UIImagePickerControllerSourceType pickerType = picker.sourceType;
if(pickerType == UIImagePickerControllerSourceTypeCamera)
{
NSDictionary *imageMetadata = [info objectForKey:
UIImagePickerControllerMediaMetadata];
// Get the assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryWriteImageCompletionBlock imageWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
if (error) {
NSLog( #"Error writing image with metadata to Photo Library: %#", error );
} else {
NSLog( #"Wrote image with metadata to Photo Library");
}
};
// Save the new image (original or edited) to the Camera Roll
[library writeImageToSavedPhotosAlbum:[imageToSave CGImage]
metadata:imageMetadata
completionBlock:imageWriteCompletionBlock];
}
}
if ([mediaType isEqualToString:#"public.movie"]) {
UIImagePickerControllerSourceType pickerType = picker.sourceType;
if(pickerType == UIImagePickerControllerSourceTypeCamera)
{
NSURL *mediaURL = [info objectForKey:UIImagePickerControllerMediaURL];
// Get the assets library
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
ALAssetsLibraryWriteVideoCompletionBlock videoWriteCompletionBlock =
^(NSURL *newURL, NSError *error) {
if (error) {
NSLog( #"Error writing image with metadata to Photo Library: %#", error );
} else {
NSLog( #"Wrote image with metadata to Photo Library");
}
};
// Save the new image (original or edited) to the Camera Roll
[library writeVideoAtPathToSavedPhotosAlbum:mediaURL
completionBlock:videoWriteCompletionBlock];
}
}
I would also like to avoid creating a custom library or custom metadata if at all possible. I really just wanted to change the filename on the way to the photo roll
I ended up answering my own question. What I wanted to do was to save to the application's documents directory instead of the photo roll. This allowed me to access the files that I had saved and also to sync them to the computer when attached later.

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