0

We are making a chat app and for video thumbnail we use the following code. But it crashes in random cases.

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) {
    }];
}

Every time it crashes on the writeVideoAtPathToSavedPhotosAlbum line and it gives only "bad access error".

Does anyone have an idea related to this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Any error message in console? – Larme Jan 30 '19 at 14:58
  • Did u check your iOS Deployment target? writeVideoAtPathToSavedPhotosAlbum:completionBlock: is only avaiable from ios 4.0-9.0 Use PHPhotoLibrary framework instead – Jabson Jan 30 '19 at 15:03
  • Hello jabson I tried to using PHPhotoLibrary library but not generate a thumbnail of the video that's why I used depicted ALAssetsLibrary method. i deployment target is iOS 10.0. so if you have any idea for generating thumbnails of HTTP URL of video then please help me. – shraddha k vaishanani Jan 31 '19 at 05:53
  • Hello Larme,not display error message only give bad access.we are enable zombie object but not display any kind of message. – shraddha k vaishanani Jan 31 '19 at 05:55
  • I have update my answer, you can use AVAssetImageGenerator class to generate thumbnail from video. there is no way to generate thumbnail from http url video should be in iPhone storage – Jabson Jan 31 '19 at 06:10
  • Hello jabson your code is working but how to save the video in particular album?.we created album "videosAPP" but video does not save in album. – shraddha k vaishanani Jan 31 '19 at 15:00

2 Answers2

1

ALAssetsLibrary library method writeVideoAtPathToSavedPhotosAlbum:completionBlock: is deprecated, you can use PHPhotoLibrary instead.

try this

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

Also check if you have Photo Library usage description in info plist with following key

NSPhotoLibraryUsageDescription

UPDATE

For fetching thumbnail from video you can use AVAssetImageGenerator class from AVFoundation framework

- (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;
}
Jabson
  • 1,585
  • 1
  • 12
  • 16
  • Hello jabson your code is working but how to save the video in particular album?.we created album "videosAPP" but video does not save in album. – shraddha k vaishanani Feb 01 '19 at 06:24
  • Hi, This is another question, next time try to create another one. please see first answer of the question: https://stackoverflow.com/questions/30249135/save-a-photo-to-custom-album-in-ios-8 – Jabson Feb 01 '19 at 09:59
0

Make sure you have permission to save to the photo gallery.

Import the Photos class:

#import <Photos/Photos.h>

Then check for Photo Library Authorization

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

}
birdman
  • 1,134
  • 13
  • 13