I want capture the video using iPhone application. In this video file i want store the local application. how to save this file in locally. Please help me.
Thanks in Advance
You can do it :
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *tempPath = [documentsDirectory stringByAppendingFormat:#"/vid1.mp4"];
BOOL success = [videoData writeToFile:tempPath atomically:NO];
[picker dismissModalViewControllerAnimated:YES];
}
Related
i want to get a photo from camera and using its name want to save in databsase...
i have used these code. i have successfully taken a picture and added in image view but now i can not able to get its name(string value)
ImagePicker = [[UIImagePickerController alloc] init];
ImagePicker.delegate = self;
ImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentModalViewController:ImagePicker animated:YES];
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
[ImagePicker dismissModalViewControllerAnimated:YES];
imageview.hidden=NO;
imageview.image = image;
NSDate *today = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:#"dd-MM-yyyy HH:mm:ss"];
NSString *dateString = [dateFormat stringFromDate:today];
// save image in document directoties
UIImage *image1=imageview.image;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0]; //Get the docs directory
NSData *pngData = UIImageJPEGRepresentation(image1,100.0);
NSString *filePath;
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"image%#.jpg",dateString]]; //Add the file name
[pngData writeToFile:filePath atomically:YES]; //Write the file
NSLog(#"File Path is %#",filePath);
if (![[NSFileManager defaultManager] fileExistsAtPath:filePath]) //Optionally check if folder already hasn't existed.
{
NSLog(#"Unable to create Folder in Documents Directory");
}
}
I guess knowing the exact image name would not be an issue rather getting a unique name for the picked image would solve your purpose so that you can save the image and track it via its name. May be this can help you
//Fetch the Maximum id from DB
NSString* strSelect= [NSString stringWithFormat:#"Select MAX(id) from TableName"];
NSMutableArray* arr_test = [[database executeQuery:strSelect]mutableCopy];
//Check if it is the first Image or not
if ([[[arr_test objectAtIndex:0]valueForKey:#"MAX(id)"]isKindOfClass:[NSNull class]])
img_id = 1;
else{
NSString *strtest = [[arr_test objectAtIndex:0] valueForKey:#"MAX(id)"];
img_id=[strtest intValue]+1;
}
//Give the Image Name with Unique ID
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
filePath = [documentsPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/image%d.jpg",img_id]];
try
-(void)imagePickerController:(UIImagePickerController *)pickerr
didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[pickerr dismissModalViewControllerAnimated:YES];
//Since we kept allowsEditing = YES , we use UIImagePickerControllerEditedImage else use UIImagePickerControllerOriginalImage
UIImage *image = [info objectForKey:#"UIImagePickerControllerEditedImage"];
[self saveImage:image];
}
(void)saveImage:(UIImage*)imagepk
{
Database *dbObj = [Database Connetion];
//NSLog(#"image width is %f",imagepk.size.width);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%d.jpg",appDelegate.activeId]];
[dbObj updateImage:[NSString stringWithFormat:#"%d.jpg",appDelegate.activeId] :appDelegate.activeId];
UIImage *image = imagepk; // imageView is my image from camera
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
}
I am new in iPhone App Development. In my code, i am able to select multiple images from the library and get the path of only 1 image. Then i am creating a copy of that image in Documents directory in a folder called "images" and trying to zip it. Now i want to get the path of all those selected multiple images, copy them in documents directory in the same folder "images" and i want to zip them later.
Please tell me how i can do the above mentioned tasks in my code.
This is how my code looks as of now:
- (void) imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (imagePickerController.allowsMultipleSelection) {
NSArray *mediaInfoArray = (NSArray *)info;
NSLog(#"Selected %d photos", mediaInfoArray.count);
NSData *webData = UIImagePNGRepresentation([[mediaInfoArray objectAtIndex:0] objectForKey:#"UIImagePickerControllerOriginalImage"]);
NSLog(#"web data length is: %u",[webData length]);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingString:#"/images.png"];
[webData writeToFile:localFilePath atomically:YES];
NSLog(#"localFilePath.%#",localFilePath);
}
I used the same QBImagePickerController and have added multiple images in an NSMutableArray using this method below. Please check, also once they are added to the array you can add them wherever you require.
- (void)imagePickerController:(QBImagePickerController *)imagePickerController didFinishPickingMediaWithInfo:(id)info
{
if(imagePickerController.allowsMultipleSelection)
{
NSArray *mediaInfoArray = (NSArray *)info;
for (int i =0; i<mediaInfoArray.count ; i++)
{
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"/Folder/Selected Files"];
dispatch_async(dispatch_get_main_queue(), ^{
NSData *data = UIImageJPEGRepresentation([[mediaInfoArray valueForKey:#"UIImagePickerControllerOriginalImage"] objectAtIndex:i], 1.0);
NSString *fileName = [stringPath stringByAppendingPathComponent:[NSString stringWithFormat:#"Image%d.png",i]];
[data writeToFile:fileName atomically:YES];
});
[ImagesArray addObject:[[mediaInfoArray valueForKey:#"UIImagePickerControllerOriginalImage"] objectAtIndex:i]];
}
}
else
{
//NSDictionary *mediaInfo = (NSDictionary *)info;
//NSLog(#"Selected: %#", mediaInfo);
}
}
[TableView reloadData];
[self dismissViewControllerAnimated:YES completion:NULL];
}
Hope this helps ...
EDIT :
Well I gave you a better solution as saving in the Cache directory is always better than in the documents directory please check is saving in NSDocumentDirectory okay?
But if you want to save it in the Documents directory anyway then you can simply replace
NSString *stringPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)objectAtIndex:0]stringByAppendingPathComponent:#"/Folder/Selected Files"];
with
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:#"savedImage.png"];
I try to save a image from camera or photo album into application directory. But i cant find what i'm doing wrong.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
theimageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"latest_photo.png"];
//extracting image from the picker and saving it
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:#"public.image"]){
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *webData = UIImagePNGRepresentation(editedImage);
[webData writeToFile:imagePath atomically:YES];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:imagePath];
if(fileExists){
NSLog(#"image Exists");
}
}
}
Why not use,
UIImage *orgImage = [info objectForKey:UIImagePickerControllerOriginalImage];
And where are failing? Did you get the image properly? or failing to write to document directory?
Check if the file in fact exist in file explorer. Maybe the compiler is right.
I have just started developing an application in which I want to auto sync all the images stored in user's photo library as assets.
I am able to get URL for images those are stored as assets.
e.g. assets-library://asset/asset.JPG?id=1000000003&ext=JPG
Now, how do I copy this image in to document's directory of my application or how do I just display image stored in assets by getting that in NSData or in UIImage?
Thanks in Advance.
You can use the following code in UIImagePickerControllerDelegate delegate implementation
- (void) imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary *)info {
//obtaining saving path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:#"my_photo.png"];
//extracting image from the picker and saving it
UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *imageData = UIImagePNGRepresentation(editedImage);
[imageData writeToFile:imagePath atomically:YES];
}
I am calling –writeToFile:atomically: on an NSData object which contains a video I just shot.
The file is not being written and if I use the version that returns an erorr object, the error is nil.
My code is:
if ([[info valueForKey:UIImagePickerControllerMediaType] isEqualToString:#"public.movie"]) {
NSURL* url = [info valueForKey:UIImagePickerControllerMediaURL];
NSData *videoData = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fPath = [documentsDirectory stringByAppendingPathComponent:#"capturedVideo.MOV"];
[videoData writeToFile:fPath atomically:YES];
}
The return value is NO and when I check the existence of the written file, it is not there.
Any ideas?
Try creating file with NSFileManager before writing to it.