如何在不使用资产库的情况下从iPhone获取视频?

时间:2015-04-28 06:42:55

标签: ios objective-c iphone ipad



- (void)showVideoList
{
    [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 = [[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];
                      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;
}
-(void)mediaPicker:(MPMediaPickerController *)mediaPicker didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
{
    MPMediaItem *item = [[mediaItemCollection items] objectAtIndex:0];
    NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];
    [mediaPicker dismissViewControllerAnimated:YES completion:nil];
    AVPlayerItem *playerItem=[AVPlayerItem playerItemWithURL:url];
    AVPlayer *player=[[AVPlayer alloc] initWithPlayerItem:playerItem];
    AVPlayerLayer *playerLayer=[AVPlayerLayer playerLayerWithPlayer:player];
    playerLayer.frame=CGRectMake(0, 0, 10, 10);
    [player play];
    [self.view.layer addSublayer:playerLayer];
}




所以我正在使用此代码,但它要求照片许可。如何在没有弹出窗口的情况下获得权限?

我正在尝试从iPhone或iPad获取视频列表,但我无法这样做。请帮帮我。

提前致谢

1 个答案:

答案 0 :(得分:0)

为什么不使用ALAssetsLibrary?这是从iPhone \ iPad获取照片/视频的推荐方法。