display all videos stored in document directory - iphone

I stored videos in my document directory from photo library. Now i want to show all videos which are stored in my document directory.. but i don't know how its possible???
Actually i want to display all videos as like its open in photo library(four videos in a single row).. and when i click on any video... the video is start playing...
can anybody help me which is the best way to show all videos from document directory to ViewController....
Thanx......
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSURL * movieURL = [info valueForKey:UIImagePickerControllerMediaURL] ;
NSData * movieData = [NSData dataWithContentsOfURL:movieURL];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[[self imageNameTextField]text]];
fullPath = [fullPath stringByAppendingFormat:#".MOV"];
[ movieData writeToFile:fullPath atomically:YES];
}

I recommend you to use an open-source grid-view control. You can find them in GitHub. For instance, BDDynamicGridViewController is interesting. But it is not the only option. There is also AQGridView.
Also, there is a popular open-source library, called Three20 and it has it's upgrade, called Nimbus. This library has a custom control for displaying photos grid. You can use the same for displaying video thumbnails grid. For instance, try this.
After you will manage to use or create Grid view control, you will need thumbnail generator for the videos. Use this topic for that purpose.

To get access to the videos stored in the photo library on the device you need to use the Asset library. The following code shows you how to get access to the first video in the photo library :
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
// Enumerate just the photos and videos group by using ALAssetsGroupSavedPhotos.
[library enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
// Within the group enumeration block, filter to enumerate just videos.
[group setAssetsFilter:[ALAssetsFilter allVideos]];
// For this example, we're only interested in the first item.
[group enumerateAssetsAtIndexes:[NSIndexSet indexSetWithIndex:0] options:0
usingBlock:^(ALAsset *alAsset, NSUInteger index, BOOL *innerStop) {
// The end of the enumeration is signaled by asset == nil.
if (alAsset) {
ALAssetRepresentation *representation = [alAsset defaultRepresentation];
NSURL *url = [representation url];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:url options:nil];
// Now you have the AV asset for the video.
}
}];
}
failureBlock: ^(NSError *error) {
// Typically you should handle an error more gracefully than this.
NSLog(#"No groups");
}];
[library release];
This example is in the AVFoundation Programming guide, more details on the Apple developer website

I have made the same project for one of my client. I can tell you the idea but cann't tell you the code. The idea is while taking or saving video take the starting frame of every video and save it as PNG image as icon of the video. By this way you will get the icon of every video. Save all the Images in different folder in such a manner that each image can be link with its video. Now retrieve all videos from the document folder by below code
NSFileManager *filemgr;
filemgr = [NSFileManager defaultManager];
filelist = [filemgr contentsOfDirectoryAtPath:path error:nil];
*filelist is the NSArray
In the same manner retrieve the icons of the videos.
Make a grid view of buttons. Set images of the buttons as icons of the videos. Show the video names. When click on the video open a new view controller and make a video player ther play the video there.

Related

File path of image on iPhone

I'm interested in uploading a file (image) from an iPhone library/camera roll to a remote web server. I already have a script working that will upload any file from the phone to a web server. However, I assume that to upload an image from the iPhone, I need the PATH to said image. Is there any way this can be done, once the user picks said image from the camera roll? I.e., how do I get the file path of an image selected in the camera roll?
I have tried to no avail.
Thanks!
You will want to look at the ALAssetsLibrary functions - these let you access photos and videos that are stored in your Photos and Videos libraries on your iOS device.
Specifically, something like:
ALAssetsLibrary *assets = [[ALAssetsLibrary alloc] init];
[assets enumerateGroupsWithTypes:ALAssetsGroupSavedPhotos
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop) {
//the ALAsset should be one of your photos - stick it in an array and after this runs, use it however you need
}
}
failureBlock:^(NSError *error) {
//something went wrong, you can't access the photo gallery
}
];
EDIT
If you are using the UIImagePickerController rather than a purely programatical approach, this simplifies it greatly:
In:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *img = [info objectForKey:UIImagePickerControllerEditedImage];
//you can use UIImagePickerControllerOriginalImage for the original image
//Now, save the image to your apps temp folder,
NSString *path = [NSTemporaryDirectory() stringByAppendingPathComponent:#"upload-image.tmp"];
NSData *imageData = UIImagePNGRepresentation(img);
//you can also use UIImageJPEGRepresentation(img,1); for jpegs
[imageData writeToFile:path atomically:YES];
//now call your method
[someClass uploadMyImageToTheWebFromPath:path];
}

saving images with UIImagePickerController

I'm trying to save Images the user takes with the camera
1) If I use the UIImageWriteToSavedPhotosAlbum I can't seem to assign the fileName that I want. How, can you choose the file name?
The nice thing about using this option is
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
Then gives a thumbnail gallery of Photo Library directory.
2) which leads me to my next question Can
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; be used to get images from your own personal directory ?
3) Is there anyway to programmatically create sub folders within the Photo Library
4) Lastly,
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSPicturesDirectory, NSUserDomainMask, YES);
if (![[NSFileManager defaultManager] fileExistsAtPath:[paths objectAtIndex:0] isDirectory:&isDir]) {
NSError *error;
[[NSFileManager defaultManager] createDirectoryAtPath:[paths objectAtIndex:0 withIntermediateDirectories:YES attributes:nil error:&error];
}
Checking to see if the NSPicturesDirectory exists so I can write to it
I keep getting a Cocoa error 513 not permitted
Thanks in advance
You can't assign a file name to photo library images. ios assign a file name called asset url to the images that are saved to the photo library, we can't change that.
If you need to save the images to photo library you can use ALAssetsLibrary
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation) [image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error)
{
// Eror
}
else
{
// Success
}
}];
[library release];
For more information check:
How to save picture to iPhone photo library?
Save image in UIImageView to iPad Photos Library
1.)
To save a filename of your choice, you need to save the image to your documents directory. This can easily be done like so:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo
{
NSString *current = [NSString stringWithFormat:#"%#/%#",PHOTODATA_PATH,currentItem];
UIGraphicsBeginImageContext(CGSizeMake(160,160));
[image drawInRect:CGRectMake(0,0,160,160)];
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData * thumbSize = UIImageJPEGRepresentation(scaledImage, 1.0);
[thumbSize writeToFile:[NSString stringWithFormat:#"%#_thumb.jpg",current] atomically:YES];
NSData * fullSize = UIImageJPEGRepresentation(image, 1.0);
[fullSize writeToFile:[NSString stringWithFormat:#"%#.jpg",current] atomically:YES];
[self dismissModalViewControllerAnimated:YES];
}
Of course this code may need to be edited to fit your exact situation but gives the basic understanding and way to save a photo to file using NSData.
2.)
Yes, UIImagePickerControllerSourceTypePhotoLibrary will access your Photo Library on your device. Any photos saved or taken from the camera roll will be accessible using this.
3.)
No, you cannot create subfolders in the Photo Library using your application. This can be done using iPhoto or iTunes or the like.
4.)
You only have access to the Documents and Library paths contained within your sandboxed environment. The only way you can save a photo to the Photo Library is by using the appropriate public methods. There is no need to check if the directory exists, the OS will take care of all of the "behind the scenes" tasks it needs to in order to manage the Photo Library.

Getting the videos from iPhone in our app

How can we get the video which are stored in the iphone library and Can we save that in our local database in our app and then we will delete the video from the iPhone Library, so that the video cannot be able to access in the iPhone library, but it can be used in the app.
NSString* mediaType = [info objectForKey:UIImagePickerControllerMediaType];
NSURL *url = [info objectForKey:UIImagePickerControllerMediaURL];
NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString * fetchPath = [documentsDirectory stringByAppendingPathComponent:#"TestDemo"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath:fetchPath] == YES) {
[fileManager removeItemAtPath:fetchPath error:nil];
}
NSError * error = nil;
if (Videourl == nil) {
return;
}
[appDelegateIphone showLoadingViewforLandScapeRight];
[[NSFileManager defaultManager] copyItemAtURL:url
toURL:[NSURL fileURLWithPath:fetchPath]
error:&error];
You can use UIImagePickerController for retrieving images and videos from the iPhone Library.
You are not able to delete any image or videos from the iPhone library.
Use the UIImagePickerController class, and set the sourceType to be UIImagePickerControllerSourceTypePhotoLibrary (this will present the user with the dialogue to choose an item from their iPhone library).
The delegate method didFinishPickingMediaWithInfo will then return a URL to the video the user has selected.
You cannot delete a video stored in the iPhone library. However, you could use the UIImagePickerController to prompt the user to record a video. You can then save this video to the file area of your app. By default it will not be saved to the iPhone library.
Check out the Apple documentation for more info.
Hope that helps.

iPhone - file properties

i m creating an application which makes the iphone work as a pendrive for easy file sharing purpose.
In the first stage, i have some files(png, pdf, jpg, zip) in a directory and i made them display in the tableview in the form of mutable array. It displays in the tableView as shown below,
.DS_Store
.localized
gazelle.pdf
Hamburger_sandwich.jpg
IITD TAJ Picture 028_jpg.jpg
iya_logo_final_b&w.jpg
manifesto09-eng.pdf
RSSReader.sql
SimpleURLConnections.zip
SQLTutorial
I just want to display the name of the files and i do not want to display the extensions. I know that it is possible to extract the extensions of a file in NSFileManager. But i do not know how. Please help me to make my table view look like this
.DS_Store
.localized
gazelle
Hamburger_sandwich
IITD TAJ Picture 028_jpg
iya_logo_final_b&w
manifesto09-eng
RSSReader
SimpleURLConnections
SQLTutorial
In the second stage i have a detailedViewController which takes displays the detailed view of the file like
file size
file type
if it is a image, it should open in imageView
if it is a song, it should play it
So i need to retrive the properties like filePath, fileType, fileSize.. of each files. Please guide me with a tutorial if possible. I too do not have any idea how to convert a mutableArray to a NSString and call the methods like stringByDeletingPathExtension. Please help me. I can even send my source code if needed. If possible, guide me with some example codes and tutorial.
This should work:)
This will get all files in a directory in a NSString *parentDirectory, get its size, if image do something otherwise it assumes is a sound file
NSFileManager *fm = [NSFileManager defaultManager];
NSError *error = nil;
NSArray *filePaths = [fm contentsOfDirectoryAtPath:parentDirectory error:&error];
if (error) {
NSLog(#"%#", [error localizedDescription]);
error = nil;
}
for (NSString *filePath in filePaths) {
//filename without extension
NSString *fileWithoutExtension = [[filePath lastPathComponent] stringByDeletingPathExtension];
//file size
unsigned long long s = [[fm attributesOfItemAtPath:[parentDirectory stringByAppendingPathComponent:filePath]
error:NULL] fileSize];
UIImage *image = [UIImage imageNamed:[parentDirectory stringByAppendingPathComponent:filePath];];
//if image...
if(image){
//show it here
}
else{
//otherwise it should be music then, play it using AVFoundation or AudioToolBox
}
}
I hope you will have the file name in NSURL object, if so then you can use the following code to get just the file name and can remove the file extension from the string.
NSArray *fileName = [[fileURL lastPathComponent] componentsSeparatedByString:#"."];
NSLog(#"%#",[fileName objectAtIndex:0]);

Getting iPhone video thumbnails

How do I get a thumbnail of a video imported from the camera roll, or the camera itself?
This has been asked before, and has been answered. However, the answers kind of suck for me.
This thread iphone sdk > 3.0 . Video Thumbnail? has some options that boil down to:
Crawl some filesystem directory for a JPG with the latest modification date that should correspond to the video you just picked. This is extremely messy, and involves rooting around in directories Apple would probably not really want me doing.
Use ffmpeg. But this is so general that I cannot seem to figure out the steps that it would take to import ffmpeg into my project and to actually call it to extract images.
Is there really no other way? This seems like a HUGE oversight in the SDK to me. I mean the video picker has thumbnails in it, so Apple must be doing something to generate those, yet does not allow us to?
-(void)testGenerateThumbNailDataWithVideo {
NSString *path = [[NSBundle mainBundle] pathForResource:#"IMG_0106" ofType:#"MOV"];
NSURL *url = [NSURL fileURLWithPath:path];
AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:url options:nil];
AVAssetImageGenerator *generate = [[AVAssetImageGenerator alloc] initWithAsset:asset];
NSError *err = NULL;
CMTime time = CMTimeMake(1, 60);
CGImageRef imgRef = [generate copyCGImageAtTime:time actualTime:NULL error:&err];
[generate release];
NSLog(#"err==%#, imageRef==%#", err, imgRef);
UIImage *currentImg = [[UIImage alloc] initWithCGImage:imgRef];
static BOOL flag = YES;
if (flag) {
NSData *tmpData = UIImageJPEGRepresentation(currentImg, 0.8);
NSString *path = [NSString stringWithFormat:#"%#thumbNail.png", NSTemporaryDirectory()];
BOOL ret = [tmpData writeToFile:path atomically:YES];
NSLog(#"write to path=%#, flag=%d", path, ret);
flag = NO;
}
[currentImg release];
}
Best method I've found... MPMoviePlayerController thumbnailImageAtTime:timeOption
Nevermind this... see first comment below. That's the answer.
We use ffmpeg, you can explore our site for hints on how to do it, eventually I want to put up a tutorial.
But right now I'm more concentrated on getting ffmpeg to play movies.
Understand once you have that code the code to generate a thumbnail is just a subset of that.
http://sol3.typepad.com/tagalong_developer_journa/
This tutorial here, has helped us and maybe the majority of developers using ffmpeg to get started.
dranger.com/ffmpeg/ "
Finally,
Apple probably would maybe not have any problems with using the thumbnail generated from the video camera, I don't think its in a private folder however that is only created by the camera and not for videos picked from the image picker.