2012-12-12 70 views
2

我已經捕獲了使用目標c的視頻,但是我無法將它保存在iphone照片庫中。我不想使用AlAssets庫,我只想使用圖像選擇器。我看到很多方法堆棧溢出和其他網站,但他們要麼使用存儲位置路徑(這是沒有提到它是什麼)或他們不工作。 這是我的一段代碼。如何使用目標c中的代碼保存視頻?

-(IBAction)Onclick:(id)sender 
{ 
    UIImagePickerController *imagePicker = [[UIImagePickerController 

              alloc] init]; 
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; 
    imagePicker.mediaTypes = [NSArray arrayWithObject:(NSString *) 

           kUTTypeMovie]; 
    imagePicker.delegate = self; 
    //UISaveVideoAtPath 
    imagePicker.allowsImageEditing = NO; 
    [self.view addSubview:imagePicker.view]; 
    [imagePicker viewWillAppear:YES]; 

    CGRect overlayFrame = CGRectMake(0, 380, 320, 44); 
    //UILabel *lbl=[[UILabel alloc]init]; 
    UIView *baseView = [[[UIView alloc] init] autorelease]; 
    baseView.backgroundColor =[UIColor greenColor]; 
    //[email protected]"dfgfdgfd"; 
    baseView.frame=overlayFrame; 
    //view.delegate = self; 
    //view.picker = picker; 
    //[view customize]; 
    imagePicker.cameraOverlayView = baseView; 


    [self presentModalViewController:imagePicker animated:YES]; 


} 


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 


    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 

    if ([mediaType isEqualToString:@"public.image"]){ 
     UIImage *picture = [info objectForKey:UIImagePickerControllerOriginalImage]; 
     UIImageWriteToSavedPhotosAlbum(picture, nil, nil, nil); 
    } 
    else if ([mediaType isEqualToString:@"public.movie"]){ 

     NSURL *url = [[[info objectForKey:UIImagePickerControllerMediaURL] copy] autorelease]; 

//  ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease]; 
//  [library writeVideoAtPathToSavedPhotosAlbum:url 
//         completionBlock:^(NSURL *assetURL, NSError *error){/*notify of completion*/}]; 
UISaveVideoAtPathToSavedPhotosAlbum(url, nil, nil, nil); 
    } 

    [self dismissModalViewControllerAnimated:YES]; 

} 

回答

7

試試這個:

- (void) imagePickerController: (UIImagePickerController *) picker 
didFinishPickingMediaWithInfo: (NSDictionary *) info 
{ 
     NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType]; 

     [self dismissViewControllerAnimated:YES completion:nil]; 

     // Handle a movie capture 
     if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) 
      == kCFCompareEqualTo) 
     { 

      NSString *moviePath = [[info objectForKey: 
            UIImagePickerControllerMediaURL] path]; 
      if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) 
      { 
       UISaveVideoAtPathToSavedPhotosAlbum (moviePath,self, @selector(video:didFinishSavingWithError:contextInfo:), nil); 
      } 


     } 
    } 
} 
- (void)video:(NSString*)videoPath didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo 
{ 
    if (error) 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Photo/Video Saving Failed" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil, nil]; 
     [alert show]; 
    } 
    else 
    { 
     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo/Video Saved" message:@"Saved To Photo Album" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil]; 
     [alert show]; 
    } 
} 

希望它可以幫助你。

+0

感謝名單@ user1853349有樂趣...... – Vishal

+0

它的正常工作......大!!!!!!!!! – Preetha

1

試試這個:

NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType]; 
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL]; 

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init]; 
[library writeVideoAtPathToSavedPhotosAlbum:videoURL completionBlock:^(NSURL *assetURL, NSError *error){ 
     /*notify of completion*/ 
     NSLog(@"AssetURL: %@",assetURL); 
     NSLog(@"Error: %@",error); 
     if (!error) { 
      //video saved 

     }else{ 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:error.domain delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil]; 
       [alert show]; 
       [alert release]; 
     } 

}];