从iOS中的画廊中选择音频和视频

时间:2015-08-07 09:39:59

标签: ios objective-c mpmovieplayer

我的问题是我必须从图库中选择音频和视频,但我的箭头是在tableview标签中选择音频路径显示,并在tableview标签中显示视频选择和视频路径,但我的箭头覆盖了tableview中的音频和视频路径。

    - (IBAction)btnPick:(id)sender {
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Media Picker Controller Media Types" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles: @"Video Library", nil];
    actionSheet.tag = 1;
    [actionSheet showInView:self.view];


     }
   - (IBAction)video:(id)sender {
      UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Media Picker Controller Media Types" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:  @"Audio Library", nil];
      flag = 1;
      actionSheet.tag = 1;
      [actionSheet showInView:self.view];

     }
     - (void)mediaPicker: (MPMediaPickerController *)mediaPicker   didPickMediaItems:(MPMediaItemCollection *)mediaItemCollection
     {
    //play your file here
    }
     - (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
    if (buttonIndex != actionSheet.cancelButtonIndex)
      {
     if (actionSheet.tag == 1)
       {
          if (flag == 1) {
            flag = 0;
            buttonIndex = 1;
        }
        mediaType = buttonIndex;
        UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"allowsPickingMultipleItems" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Yes",@"No", nil];
        actionSheet.tag = 2;
        [actionSheet showInView:self.view];
         }
        else if (actionSheet.tag == 2)
        {
        IQMediaPickerController *controller = [[IQMediaPickerController alloc] init];
        controller.delegate = self;
        [controller setMediaType:mediaType];
        controller.allowsPickingMultipleItems = (buttonIndex == 0);
        [self presentViewController:controller animated:YES   completion:nil];
        }
     }

   }
   - (void)mediaPickerController:(IQMediaPickerController*)controller didFinishMediaWithInfo:(NSDictionary *)info;
  {
      NSLog(@"Info: %@",info);

       mediaInfo = [info copy];

       [tblview reloadData];

     }

    -(void)mediaPickerControllerDidCancel:(IQMediaPickerController  *)controller;
    {
    }

   -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
   {
     return [mediaInfo count];
   }

   -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
    {
    key = [[mediaInfo allKeys] objectAtIndex:section];
    return [[mediaInfo objectForKey:key] count];
   }

     -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
     {
      key = [[mediaInfo allKeys] objectAtIndex:indexPath.section];

      if ([key isEqualToString:IQMediaTypeImage])
       {
        return 80;
       }

      else
       {
       return tableView.rowHeight;
       }

    }
   -(UITableViewCell *)tableView:(UITableView *)tableView  cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {

     static NSString *cellIdentifire = @"Cell";

     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifire];
     if (cell == nil) {
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifire];
     }


       key = [[mediaInfo allKeys] objectAtIndex:indexPath.section];
       dict = [[mediaInfo objectForKey:key] objectAtIndex:indexPath.row];

      [dict objectForKey:IQMediaItem];

     MPMediaItem *item = [dict objectForKey:IQMediaItem];
     UILabel *lblname2 = (UILabel *)[cell viewWithTag:2];
     lblname2.text = [[item valueForProperty:MPMediaItemPropertyAssetURL] relativePath];


      if ([dict objectForKey:IQMediaAssetURL]) {

     NSURL*  url = [dict objectForKey:IQMediaAssetURL];
     UILabel *lblname = (UILabel *)[cell viewWithTag:1];
     lblname.text = [url relativePath];
     }

     return cell;

   }
  -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:  (NSIndexPath *)indexPath
   {
    key = [[mediaInfo allKeys] objectAtIndex:indexPath.section];

    dict = [[mediaInfo objectForKey:key] objectAtIndex:indexPath.row];

    if ([dict objectForKey:IQMediaItem])
     {
       MPMediaItem *item = [dict objectForKey:IQMediaItem];
       NSURL *url = [item valueForProperty:MPMediaItemPropertyAssetURL];

      MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
     [self presentMoviePlayerViewControllerAnimated:controller];

    }
       else if([dict objectForKey:IQMediaAssetURL])
     {
       NSURL *url = [dict objectForKey:IQMediaAssetURL];

     MPMoviePlayerViewController *controller = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [self presentMoviePlayerViewControllerAnimated:controller];
      }

      else if ([dict objectForKey:IQMediaURL])
       {
       NSURL *url = [[[mediaInfo objectForKey:key] objectAtIndex:indexPath.row] objectForKey:IQMediaURL];

       MPMoviePlayerViewController *controller =  [[MPMoviePlayerViewController alloc] initWithContentURL:url];
      [self presentMoviePlayerViewControllerAnimated:controller];
     }

      [tableView deselectRowAtIndexPath:indexPath animated:YES];
     }

0 个答案:

没有答案