应用程序崩溃:使用 ALAssetsLibrary 写入 VideoAtPathToSavedPhotosAlbum 时

App crash: when writeVideoAtPathToSavedPhotosAlbum using 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 行崩溃并且只给出 "bad access error".

有人对此有想法吗?

ALAssetsLibrary 库方法writeVideoAtPathToSavedPhotosAlbum:completionBlock:已弃用,您可以改用PHPhotoLibrary。

试试这个

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

同时检查信息 plist 中是否有照片库使用说明

NSPhotoLibraryUsageDescription

更新

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

- (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;
}

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

导入照片 class:

#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];

}