IOS development-Get local video files, ios development get videos

Source: Internet
Author: User

IOS development-Get local video files, ios development get videos

The following describes the implementation process.
First.
Figure 1. iTunes

Figure 2. iTunes

Figure 3. Run

Now, we can see the effect through the image.
Functions: allows you to import files through iTunes. You can view all files in the sandbox.

Implementation process:
1. Add the UIFileSharingEnabled key to the Info. plist file of the application, and set the key value to YES.

2. Code:
ViewController. h

Import <UIKit/UIKit. h>

// Step1. import the QuickLook library and header file

Import <QuickLook/QuickLook. h>

// Step2. Inheritance Protocol
@ Interface ViewController: UIViewController <UITableViewDataSource, UITableViewDelegate, QLPreviewControllerDataSource, QLPreviewControllerDelegate, UIDocumentInteractionControllerDelegate>
{
// Step3. declare and display the list
IBOutlet UITableView * readTable;
}

// Setp4. declare the variable
// UIDocumentInteractionController: A file interaction controller that supports application management and file user interaction in the local system.
// DirArray: stores all files in the sandbox.
@ Property (nonatomic, retain) NSMutableArray * dirArray;
@ Property (nonatomic, strong) UIDocumentInteractionController * docInteractionController;
@ End

ViewController. m

-(Void) viewDidLoad{

[Super viewDidLoad];

// Step 5. save an image to the document folder of the device (for test convenience). UIImage * image = [UIImage imageNamed: @ "testPic.jpg"]; NSData * jpgData = UIImageJPEGRepresentation (image, 0.8); NSArray * paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentsPath = [paths objectAtIndex: 0]; // Get the docs directory NSString * filePath = [documentsPath stringByAppendingPathComponent: @ "testPic.jpg"]; // Add the file name [jpgData writeToFile: filePath atomically: YES]; // Write the file // step5. save a txt file to the document folder of the device (for test convenience) char * saves = "Colin_csdn "; NSData * data = [[NSData alloc] initWithBytes: saves length: 10]; filePath = [documentsPath stringByAppendingPathComponent: @ "colin.txt"]; [data writeToFile: filePath atomically: YES]; // step6. obtain all the files in the sandbox NSFileManager * fileManager = [NSFileManager defaultManager]; // obtain the list of files and folders in the application Documents folder NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentDir = [documentPaths objectAtIndex: 0]; NSError * error = nil; NSArray * fileList = [[NSArray alloc] init]; // fileList is the array fileList = [fileManager contentsOfDirectoryAtPath: documentDir error: & error]; self. dirArray = [[NSMutableArray alloc] init]; for (NSString * file in fileList) {[self. dirArray addObject: file];}

// Step 6. Refresh the list and display the data
[ReadTable reloadData];
}

// Step7. use the url path to open UIDocumentInteractionController

-(Void) setupDocumentControllerWithURL :( NSURL *) url
{
If (self.doc InteractionController = nil)
{
Self.doc InteractionController = [UIDocumentInteractionController interactionControllerWithURL: url];
Self.doc InteractionController. delegate = self;
}
Else
{
Self.doc InteractionController. URL = url;
}
}

Pragma mark-List Operation
  • (NSInteger) numberOfSectionsInTableView :( UITableView *) tableView
    {
    Return 1;
    }
  • (UITableViewCell *) tableView :( UITableView *) tableView cellForRowAtIndexPath :( NSIndexPath *) indexPath
    {
    Static NSString * CellName = @ "CellName ";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier: CellName];
    If (cell = nil)
    {
    Cell = [[UITableViewCell alloc] initWithStyle: UITableViewCellStyleDefault reuseIdentifier: CellName];
    Cell. accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }

    NSURL * fileURL = nil;
    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
    NSString * documentDir = [documentPaths objectAtIndex: 0];
    NSString * path = [documentDir stringByAppendingPathComponent: [self. dirArray objectAtIndex: indexPath. row];
    FileURL = [NSURL fileURLWithPath: path];
    [Self setupDocumentControllerWithURL: fileURL];
    Cell. textLabel. text = [self. dirArray objectAtIndex: indexPath. row];
    NSInteger iconCount = [self.doc InteractionController. icons count];
    If (iconCount> 0)
    {
    Cell. imageView. image = paiself.doc InteractionController. icons objectAtIndex: iconCount-1];
    }

    Return cell;
    }

  • (NSInteger) tableView :( UITableView *) tableView numberOfRowsInSection :( NSInteger) section
    {
    Return [self. dirArray count];
    }

  • (Void) tableView :( UITableView *) tableView didSelectRowAtIndexPath :( NSIndexPath *) indexPath
    {
    QLPreviewController * previewController = [[QLPreviewController alloc] init];
    PreviewController. dataSource = self;
    PreviewController. delegate = self;

    // Start previewing the document at the current section index
    PreviewController. currentPreviewItemIndex = indexPath. row;
    [[Self navigationController] pushViewController: previewController animated: YES];
    // [Self presentViewController: previewController animated: YES completion: nil];
    }

Pragma mark-UIDocumentInteractionControllerDelegate
  • (NSString *) applicationDocumentsDirectory
    {
    Return [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    }

  • (UIViewController *) documentInteractionControllerViewControllerForPreview :( UIDocumentInteractionController *) interactionController
    {
    Return self;
    }

Pragma mark-QLPreviewControllerDataSource

// Returns the number of items that the preview controller shold preview

  • (NSInteger) numberOfPreviewItemsInPreviewController :( QLPreviewController *) previewController
    {
    Return 1;
    }

  • (Void) previewControllerDidDismiss :( QLPreviewController *) controller
    {
    // If the preview dismissed (done button touched), use this method to post-process previews
    }

// Returns the item that the preview controller shoshould preview

  • (Id) previewController :( QLPreviewController *) previewController previewItemAtIndex :( NSInteger) idx
    {
    NSURL * fileURL = nil;
    NSIndexPath * selectedIndexPath = [readTable indexPathForSelectedRow];
    NSArray * documentPaths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES );
    NSString * documentDir = [documentPaths objectAtIndex: 0];
    NSString * path = [documentDir stringByAppendingPathComponent: [self. dirArray objectAtIndex: selectedIndexPath. row];
    FileURL = [NSURL fileURLWithPath: path];
    Return fileURL;
    }
  • (Void) didReceiveMemoryWarning
    {
    [Super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

@ End

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.