Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple images not getting saved in Photo Library by using UIActivityViewController

I need to save multiple images in the photo library, the user can multiple selects the images from the app gallery then can save them in iPhone Photo Gallery. I am showing the UIActivityViewController for the purpose.

Suppose a user selects 10 or more images and choose to save them into photo library then only 7-8 images are saved.

Is there any way by which i can save array of images in the photo library without any failure ?

Thanks

let images = Generic.fetchImagesFromMediaFiles(self.selectedMediaObj) // to fetch selected images

let activityViewController = UIActivityViewController(activityItems: images, applicationActivities: nil)
self.present(activityViewController, animated: true, completion: nil);

if let popoverPresentationController = activityViewController.popoverPresentationController {
    popoverPresentationController.sourceView = self.shareAllView
}
like image 678
Mr. Bean Avatar asked Aug 17 '18 12:08

Mr. Bean


2 Answers

iOS system write photo save to album use single thread, one by one to do. if you want to save more photos same time, it maybe loss some data.

-(void)saveBtn
{
[SSGOTools againRequestPhotoWithblock:^(BOOL isAgree) {
if (isAgree) {

self.listOfImages = [NSMutableArray new];
int photoNum ;
photoNum = (int)_photoArray.count;
if (_photoArray.count > 9) {
photoNum = 9;
}
for (int i = 0; i < photoNum; i++) {
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_photoArray[i]]];
UIImage *myImage = [UIImage imageWithData:data];
//[self.listOfImages addObject:myImage];
[self loadImageFinished:myImage];

}
}
}];
}

- (void)loadImageFinished:(UIImage *)image
{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

//write photo save to album

[PHAssetChangeRequest creationRequestForAssetFromImage:image];

} completionHandler:^(BOOL success, NSError * _Nullable error) {

NSLog(@"success = %d, error = %@", success, error);
if(success){
dispatch_async(dispatch_get_main_queue(), ^{
[SSGOTools showInfoPopHint:@"Success"];
});
}
}];
}
like image 125
Sean Avatar answered Oct 25 '22 23:10

Sean


you will need to use the completion block here for ensuring all images are saved.. try this :

-(void)saveBtn{
[SSGOTools againRequestPhotoWithblock:^(BOOL isAgree) {
    if (isAgree) {
        self.listOfImages = [NSMutableArray new];
        int photoNum ;
        photoNum = (int)_photoArray.count;
        if (_photoArray.count > 9) {
            photoNum = 9;
        }
        for (int i = 0; i < photoNum; i++) {
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:_photoArray[i]]];
            UIImage *myImage = [UIImage imageWithData:data];
            [self.listOfImages addObject:myImage];
           // [self loadImageFinished:myImage];
        }
       [self saveAllImages:self.listOfImages];
    }
}];
}
-(void)saveAllImages:(NSMutableArray *)images {
UIImage *image = [images firstObject];
[images removeObject:image];

[self loadImageFinished:image :^(bool success) {

    if (success){

        if (images.count > 0){
            [self saveAllImages:images];
        }else{
            // all images saved do whatever you want;
        }

    }else{
        NSLog(@"failed saving image");
    }

}];
}
- (void)loadImageFinished:(UIImage *)image :(void(^)(bool success))completion{
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{

    //write photo save to album

    [PHAssetChangeRequest creationRequestForAssetFromImage:image];

} completionHandler:^(BOOL success, NSError * _Nullable error) {

    NSLog(@"success = %d, error = %@", success, error);
    if(success){
        dispatch_async(dispatch_get_main_queue(), ^{
            [SSGOTools showInfoPopHint:@"Success"];
        });
    }
    completion(success);
}];
}
like image 2
Shahzaib Qureshi Avatar answered Oct 25 '22 23:10

Shahzaib Qureshi