如何将照片的幻灯片保存到相机角色



如何保存在相机卷中创建的幻灯片?

(NSString *))handler;
{
    AVMutableComposition* mixComposition = [AVMutableComposition composition];
    AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:videoPath] options:nil];
    NSError * error = nil;
    for (int i=0;i<[arrayOfSounds count];i++)
    {
        NSString *pathString = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%@", [arrayOfSounds objectAtIndex:i]] ofType:@"mp3"];
        NSLog(@"pathString = %@",pathString);
        AVURLAsset * urlAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:pathString] options:nil];
        AVAssetTrack * audioAssetTrack = [[urlAsset tracksWithMediaType:AVMediaTypeAudio] objectAtIndex:0];
        AVMutableCompositionTrack *compositionAudioTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeAudio
                                                                                       preferredTrackID: kCMPersistentTrackID_Invalid];
        CMTime audioDuration = videoAsset.duration;
        audioDurationSeconds = CMTimeGetSeconds(audioDuration);
        int startDur= [[arrayOfTime objectAtIndex:i] intValue];
        NSLog(@"startDur = %d",startDur);
        CMTime audioStartTime = CMTimeMake(i,1);
        CMTime presentTime    = CMTimeMake(1, 1);
        CMTime EndTime        = CMTimeMake(audioDurationSeconds, 1);
        CMTime audioEndTime   = CMTimeAdd(presentTime, EndTime);
     [compositionAudioTrack insertTimeRange:CMTimeRangeMake(presentTime,audioEndTime) ofTrack:audioAssetTrack atTime:audioStartTime error:&error];
    }
    NSString* movPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,@"Export.mov"];
    //AVURLAsset* videoAsset = [AVURLAsset URLAssetWithURL:[NSURL fileURLWithPath:movPath] options:nil];
    AVMutableCompositionTrack *compositionVideoTrack = [mixComposition addMutableTrackWithMediaType:AVMediaTypeVideo preferredTrackID:kCMPersistentTrackID_Invalid];
    NSError* vidError;
    [compositionVideoTrack insertTimeRange:CMTimeRangeMake(kCMTimeZero, videoAsset.duration) ofTrack:([videoAsset tracksWithMediaType:AVMediaTypeVideo].count >0)? [[videoAsset tracksWithMediaType:AVMediaTypeVideo] objectAtIndex:0]:nil atTime:kCMTimeZero error:&vidError];
    NSLog(@"Vid error = %@",vidError);
    AVAssetExportSession* _assetExport = [[AVAssetExportSession alloc] initWithAsset:mixComposition presetName:AVAssetExportPresetPassthrough];
    NSString* videoName = @"exportFinal.mov";
    NSString *exportPath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,videoName];
    NSLog(@"exportPath = %@",exportPath);
    NSURL *exportUrl = [NSURL fileURLWithPath:exportPath];
    if ([[NSFileManager defaultManager] fileExistsAtPath:exportPath])
    {
        [[NSFileManager defaultManager] removeItemAtPath:exportPath error:nil];
    }
    _assetExport.outputFileType = @"com.apple.quicktime-movie";
    NSLog(@"file type %@",_assetExport.outputFileType);
    _assetExport.outputURL = exportUrl;
    _assetExport.shouldOptimizeForNetworkUse = YES;
    [_assetExport exportAsynchronouslyWithCompletionHandler:
     ^(void ) {
         switch (_assetExport.status)
         {
             case AVAssetExportSessionStatusCompleted:
                 //export complete
                 NSLog(@"Export Complete");
                 //[self uploadToYouTube];
                 [self cleanUpProcess];
                 break;
             case AVAssetExportSessionStatusFailed:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
                 //export error (see exportSession.error)
                 break;
             case AVAssetExportSessionStatusCancelled:
                 NSLog(@"Export Failed");
                 NSLog(@"ExportSessionError: %@", [_assetExport.error localizedDescription]);
                 //export cancelled
                 break;
         }
     }];
}
- (void)cleanUpProcess{
    NSError* error;
    NSArray *files = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:&error];
    for(NSString* myFiles in files){
        if([[myFiles pathExtension] isEqualToString:@"mov"]){
            continue;
        }
        NSString* filePath = [NSString stringWithFormat:@"%@/%@",documentsDirectory,myFiles];
        if(![[NSFileManager defaultManager] removeItemAtPath:filePath error:&error]){
            NSLog(@"Error Deleting");
        }
    }
}

它保存在此路径中:

" user/devendrasingh/library/developer/coreSimulator/decession/394ad1c9-e950-422e-bda1-083cdcee83f6/data/data/data/contains/data/data/application/application/application/application/napplication/napplication/98bec162-c686-bc1-bc1-bc1-bc1-bc1-bc1-bc1-bc1-bc1-bc698-567blint inmm "

它没有保存在相机卷中

尝试以下:

[[[ALAssetsLibrary alloc] init] writeVideoAtPathToSavedPhotosAlbum:outputFileURL completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error)
            NSLog(@"%@", error);
        [[NSFileManager defaultManager] removeItemAtURL:outputFileURL error:nil];
    }];

最新更新