应用崩溃:使用ALAssetsLibrary的writeVideoAtPathToSavedPhotosAlbum时

时间:2019-01-30 14:40:13

标签: ios objective-c alassetslibrary

我们正在制作一个聊天应用程序,对于视频缩略图,我们使用以下代码。但是它在随机情况下崩溃。

NSArray *arrjid = [jid componentsSeparatedByString:@"@"];
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyyMMddHHmmss"];
NSString *strdate = [dateFormatter stringFromDate:[NSDate date]];
NSString *strname = [NSString stringWithFormat:@"%@_%@_file.mov",arrjid[0],strdate];
NSString *videoPath = [[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:strname];
[videoData writeToFile:videoPath atomically:YES];

if([[NSFileManager defaultManager] fileExistsAtPath:videoPath])
{
    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:videoPath] completionBlock:^(NSURL *assetURL, NSError *error) {
    }];
}

每次它在writeVideoAtPathToSavedPhotosAlbum行上崩溃,并且仅给出“访问错误”。

有人有与此相关的想法吗?

2 个答案:

答案 0 :(得分:1)

ALAssetsLibrary库方法writeVideoAtPathToSavedPhotosAlbum:completionBlock:已过时,可以改用PHPhotoLibrary。

尝试

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
    [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL: yourVideoURlHere];
} completionHandler:^(BOOL success, NSError *error) {
    if (success) {
        //Do Something   
    }
}];

还要通过以下键检查信息plist中是否有照片库使用情况描述

NSPhotoLibraryUsageDescription

更新

要从视频中获取缩略图,可以使用AVFoundation框架中的AVAssetImageGenerator类

- (UIImage *) thumbnailFromVideoAtURL:(NSURL *) contentURL {
    AVURLAsset *asset = [[AVURLAsset alloc] initWithURL:contentURL options:nil];
    AVAssetImageGenerator *generator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
    generator.appliesPreferredTrackTransform = YES;
    NSError *err = NULL;
    CMTime time = CMTimeMake(1, 60);
    CGImageRef imgRef = [generator copyCGImageAtTime:time actualTime:NULL error:&err];
    UIImage *thumbnail = [[UIImage alloc] initWithCGImage:imgRef];
    CGImageRelease(imgRef);

    return thumbnail;
}

答案 1 :(得分:0)

确保您有权保存到照片库。

导入Photos类:

#import <Photos/Photos.h>

然后检查照片库授权

PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];

if (status == PHAuthorizationStatusAuthorized) {
    //OK to save your video
    [self ContinueDownload];
}
else if (status == PHAuthorizationStatusDenied) {
    // Access has been denied.
    [self ShowAlert];
}
else if (status == PHAuthorizationStatusNotDetermined) {

    // Access has not been determined.
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {

        if (status == PHAuthorizationStatusAuthorized) {
             //OK to save your video
            [self ContinueDownload];
        }
        else {
            // Access has been denied.
            [self ShowAlert];
        }
    }];
}
else if (status == PHAuthorizationStatusRestricted) {
    // Restricted access
}

//Show an alert if access is denied
-(void)ShowAlert {
    UIAlertController * alert = [UIAlertController
                                 alertControllerWithTitle:@"Gallery Access denied"
                                 message:@"You can grant access in\nSettings/Privacy/Photos\nif you change your mind."
                                 preferredStyle:UIAlertControllerStyleAlert];



    UIAlertAction* OKButton = [UIAlertAction
                               actionWithTitle:@"OK"
                               style:UIAlertActionStyleDefault
                               handler:^(UIAlertAction * action) {
                                   //[self dismissViewControllerAnimated:YES completion:nil];
                               }];

    [alert addAction:OKButton];

    [self presentViewController:alert animated:YES completion:nil];

}