Xin chào Tôi đang làm việc Trên video, tôi muốn nhận danh sách tệp video từ thư viện để Hiển thị và phát Video trong ứng dụng của tôi. Ai giúp tôi với.Làm thế nào tôi có thể lấy danh sách tất cả các tệp video từ thư viện trong ios sdk
Trả lời
allVideos = [[NSMutableArray alloc] init];
ALAssetsLibrary *assetLibrary = [[ALAssetsLibrary alloc] init];
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
dic = [[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
NSURL *videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
NSString *title = [NSString stringWithFormat:@"video %d", arc4random()%100];
UIImage *image = [self imageFromVideoURL:videoURL];
[dic setValue:image forKey:@"image"];
[dic setValue:title forKey:@"name"];
[dic setValue:videoURL forKey:@"url"];
[allVideos addObject:dic];
}
}];
else
{
}
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
Nó sẽ mở thư viện ảnh và chỉ hiển thị nội dung loại phim.
#import <MobileCoreServices/MobileCoreServices.h>
UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeMovie, nil];
[self presentModalViewController:imagePicker animated:YES];
Vui lòng tham khảo liên kết bên dưới. Có thể hữu ích cho bạn
http://www.raywenderlich.com/13418/how-to-play-record-edit-videos-in-ios
Chúc may mắn !!!
Trong khi điều này về lý thuyết có thể trả lời câu hỏi, [nó sẽ là thích hợp hơn] (http: //meta.stackexchange.com/q/8259) để bao gồm các phần thiết yếu của câu trả lời ở đây và cung cấp liên kết để tham khảo. Bạn cũng cần giải thích * cách * nó trả lời câu hỏi. –
sử dụng phương pháp này Để chọn video Từ Thư viện.
//Call this method.
[self startMediaBrowserFromViewController: self usingDelegate: self];
- (BOOL) startMediaBrowserFromViewController: (UIViewController*) controller usingDelegate: (id <UIImagePickerControllerDelegate, UINavigationControllerDelegate>) delegate{
if (([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeSavedPhotosAlbum] == NO) || (delegate == nil) || (controller == nil))
return NO;
UIImagePickerController *mediaUI = [[UIImagePickerController alloc] init];
mediaUI.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
mediaUI.mediaTypes = [[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil] autorelease];
mediaUI.allowsEditing = YES;
mediaUI.delegate = self;
mediaUI.videoMaximumDuration = 60.0;
//mediaUI.videoQuality = UIImagePickerControllerQualityTypeLow;
[controller presentModalViewController: mediaUI animated: YES];
return YES;
}
Nhận Danh sách tất cả các video và Thumbnail
Nhờ @Nikhil, người chia sẻ thông tin here, nó đã giúp tôi, nhưng nó vẫn cần vài giờ để làm cho mã có thể thực thi được vì thiếu một số thứ trong câu trả lời của mình
Vì vậy, tôi muốn chia sẻ mã làm việc đầy đủ của tôi
1.just thêm khung AssetsLibrary, AVFoundation và MediaPlayer.
2. AssetBrowserItem.h và AssetBrowserItem.mhere
3.Use mã dưới đây để có được danh sách của tất cả các video từ thiết bị ios lib
4.run ứng dụng và xem Log cho video các chi tiết
#import "HomeViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "AssetBrowserItem.h"
@interface HomeViewController()
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;
@property (nonatomic, strong) NSURL *videoURL;
@property (nonatomic, strong) MPMoviePlayerController *mpVideoPlayer;
@property (nonatomic, strong) NSMutableArray *videoURLArray;
@property (nonatomic, strong) NSMutableArray *assetItems;
@property (nonatomic, strong) NSMutableDictionary *dic;
@end
@implementation HomeViewController
@synthesize assetsLibrary, assetItems,dic;
@synthesize videoURL,videoURLArray, mpVideoPlayer;
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#pragma mark - Show Video List Methods
- (IBAction)showVideoList:(id)sender
{
[self buildAssetsLibrary];
}
- (void)buildAssetsLibrary
{
assetsLibrary = [[ALAssetsLibrary alloc] init];
ALAssetsLibrary *notificationSender = nil;
videoURLArray = [[NSMutableArray alloc] init];
NSString *minimumSystemVersion = @"4.1";
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
if ([systemVersion compare:minimumSystemVersion options:NSNumericSearch] != NSOrderedAscending)
notificationSender = assetsLibrary;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(assetsLibraryDidChange:) name:ALAssetsLibraryChangedNotification object:notificationSender];
[self updateAssetsLibrary];
}
- (void)assetsLibraryDidChange:(NSNotification*)changeNotification
{
[self updateAssetsLibrary];
}
- (void)updateAssetsLibrary
{
assetItems = [NSMutableArray arrayWithCapacity:0];
ALAssetsLibrary *assetLibrary = assetsLibrary;
[assetLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop)
{
if (group)
{
[group setAssetsFilter:[ALAssetsFilter allVideos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index, BOOL *stop)
{
if (asset)
{
dic = [[NSMutableDictionary alloc] init];
ALAssetRepresentation *defaultRepresentation = [asset defaultRepresentation];
NSString *uti = [defaultRepresentation UTI];
videoURL = [[asset valueForProperty:ALAssetPropertyURLs] valueForKey:uti];
mpVideoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
NSString *title = [NSString stringWithFormat:@"%@ %lu", NSLocalizedString(@"Video", nil), [assetItems count]+1];
[self performSelector:@selector(imageFromVideoURL)];
[dic setValue:title forKey:@"VideoTitle"];//kName
[dic setValue:videoURL forKey:@"VideoUrl"];//kURL
AssetBrowserItem *item = [[AssetBrowserItem alloc] initWithURL:videoURL title:title];
[assetItems addObject:item];
[videoURLArray addObject:dic];
NSLog(@"Video has info:%@",videoURLArray);
}
NSLog(@"Values of dictonary==>%@", dic);
//NSLog(@"assetItems:%@",assetItems);
NSLog(@"Videos Are:%@",videoURLArray);
} ];
}
// group == nil signals we are done iterating.
else
{
dispatch_async(dispatch_get_main_queue(), ^{
//[self updateBrowserItemsAndSignalDelegate:assetItems];
// loadImgView.hidden = NO;
// [spinner stopAnimating];
// [loadImgView removeFromSuperview];
//selectVideoBtn .userInteractionEnabled = YES;
});
}
}
failureBlock:^(NSError *error)
{
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
}
- (UIImage *)imageFromVideoURL
{
UIImage *image = nil;
AVAsset *asset = [[AVURLAsset alloc] initWithURL:videoURL options:nil];;
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
// calc midpoint time of video
Float64 durationSeconds = CMTimeGetSeconds([asset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
// get the image from
NSError *error = nil;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:midpoint actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
// cgimage to uiimage
image = [[UIImage alloc] initWithCGImage:halfWayImage];
[dic setValue:image forKey:@"ImageThumbnail"];//kImage
NSLog(@"Values of dictonary==>%@", dic);
NSLog(@"Videos Are:%@",videoURLArray);
CGImageRelease(halfWayImage);
}
return image;
}
@end
Thanh toán các bài viết trên blog để lấy toàn bộ tài sản video sử dụng hình ảnh Khung http://iavinashkashyap.blogspot.in/2016/11/get-list-of-all-videos.html
Code:
- (void) {getAllVideoAssets
NSMutableArray *assets = [[NSMutableArray alloc] init];
//Fetch all video assets from Photos
PHFetchResult *assetResults = [PHAsset fetchAssetsWithMediaType:PHAssetMediaTypeVideo options:nil];
//get all assets
for (PHAsset *asset in assetResults){
NSLog(@"asset type = %zd", asset.mediaType);
[assets addObject:asset];
}
self.allVideoslistArray = [[NSMutableArray alloc] init];
//create an instance of PHImageManager
PHImageManager *manager = [PHImageManager defaultManager];
for(PHAsset *asset in assets){
//block of code for represent video assets
[manager requestAVAssetForVideo:asset options:nil resultHandler:^(AVAsset * _Nullable asset, AVAudioMix * _Nullable audioMix, NSDictionary * _Nullable info) {
if ([asset isKindOfClass:[AVURLAsset class]]) {
NSURL *url = (NSURL *)[[(AVURLAsset *)asset URL] fileReferenceURL];
UIImage *thumbnail = [self createThunbnailImage:url];
[self.allVideoslistArray addObject:@{@"VideoUrl":url,@"ThumbnailImage":thumbnail, @"VideoAsset":asset}];
}
}];
}//end for loop
}
Sẽ tuyệt vời nếu bạn cung cấp phiên bản Swift 3? –
@Anura Tôi sẽ thực hiện tương tự trong Swift. – avi
- 1. Tôi có thể lấy danh sách tất cả các phần tử iOS ở đâu?
- 2. Cách lấy danh sách tất cả các tệp video trong iPhone
- 3. Cách lấy danh sách tệp (*. Jpg) từ tất cả các thư mục?
- 4. Android, làm cách nào để có danh sách tất cả các tệp trong một thư mục?
- 5. Làm thế nào để liệt kê tất cả các thư mục và thư mục con/tệp trong iPhone SDK?
- 6. Làm cách nào để lấy danh sách tất cả các tệp có phần mở rộng ESY trong một thư mục?
- 7. Làm thế nào tôi có thể lấy danh sách thư mục trước sau khi mở một tệp từ danh sách thư mục/tệp?
- 8. Lấy danh sách tất cả các tệp trong thư mục (đệ quy)
- 9. Làm cách nào tôi có thể lấy tệp tininfo của tất cả các tệp trong một thư mục bằng GetFile()?
- 10. Lấy danh sách tất cả các tập tin bên trong một thư mục trong vb.net
- 11. Delphi - cách lấy danh sách tất cả các tệp của thư mục
- 12. danh sách PHP tất cả các file trong thư mục
- 13. Có thể lấy danh sách các tệp trong thư mục của trang web không? Làm sao?
- 14. Làm cách nào để có danh sách tất cả tên tệp trong thư mục bằng VB6?
- 15. Flash AS3 - Lấy danh sách các đối tượng Thư viện?
- 16. Lấy Danh sách tất cả các Màn hình Xorg
- 17. iPhone có danh sách tất cả SSID không có thư viện riêng
- 18. android dexclassloader lấy danh sách tất cả các lớp
- 19. Tìm tệp trong tất cả các thư mục có thể?
- 20. Làm thế nào tôi có thể tìm thấy tất cả các kết hợp có thể có của một danh sách các danh sách (bằng Python)?
- 21. Liệt kê tất cả các tệp từ tất cả thư mục trong một danh sách duy nhất
- 22. Lấy danh sách các thư viện ảnh trên Android
- 23. cách lấy danh sách tất cả các tệp đang mở trong trình chỉnh sửa nhật thực?
- 24. Làm thế nào để có được danh sách thư viện chia sẻ thực thi từ C++?
- 25. Lấy danh sách tệp từ thư mục SkyDrive (Windows Phone)
- 26. Làm cách nào để lấy danh sách và tạo tất cả các danh sách có độ dài ngày càng tăng?
- 27. Làm cách nào để tìm tất cả các phần tử có thể có trong danh sách?
- 28. Có cách nào để lấy tất cả các tệp từ một đốm màu xanh da trời
- 29. Làm thế nào để loại bỏ tất cả các chuỗi thoát khỏi danh sách các chuỗi?
- 30. Tôi đã xóa tất cả các tệp trong thư mục của mình. Làm thế nào tôi có thể lấy lại chúng?
Bạn có cố gắng bất cứ điều gì? –
Có thể điều này sẽ giúp bạn: http://stackoverflow.com/questions/8301950/how-to-get-the-list-of-all-gallery-images-in-ios – OMK
@NagRaj một số người hỏi bạn. một số người trả lời bạn. plz phát lại. Nếu bạn muốn giúp đỡ về SO. –