将应用内视频保存到相机胶卷?

时间:2012-05-02 03:41:10

标签: iphone video camera

是否可以拍摄嵌入在应用中的视频并将其保存到相机胶卷?如果是这样,怎么样?它是否与UIWriteImage相同?

2 个答案:

答案 0 :(得分:11)

您可以尝试使用此代码,假设您已经知道如何保存图像:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
       //Save the video
       NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
       UISaveVideoAtPathToSavedPhotosAlbum([movieUrl relativePath], self,@selector(movie:didFinishSavingWithError:contextInfo:), nil);
    }
}

或者你可以试试:

-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  if([mediaType isEqualToString:(NSString *)kUTTypeMovie]) {
    NSURL *movieUrl = [info objectForKey:UIImagePickerControllerMediaURL];
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:movieUrl completionBlock:^(NSURL *assetURL, NSError *error){
        if(error) {
            NSLog(@"CameraViewController: Error on saving movie : %@ {imagePickerController}", error);
        }
        else {
            NSLog(@"URL: %@", assetURL);
        }
    }];
  }
}

答案 1 :(得分:2)

对于iOS 8及更高版本,请使用PHPhotoLibrary ALAssetsLibrary 已弃用。

包括:

@import PhotosUI;

然后用它来保存你的电影:

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^
 {
     [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:<%Your Movie URL%>];
 }
                                  completionHandler:^(BOOL success, NSError *error)
 {
     if (success) {
         NSLog(@"Movie saved to camera roll.");
     }
     else {
         NSLog(@"Could not save movie to camera roll. Error: %@", error);
     }
 }];