简体   繁体   中英

How to save URL images to a gallery in IOS

I have an array of images coming from sever. I have a button to save it to gallery. The images coming from server are in url form. Now i want to download the image which i select from an array. I have a button when then image opens in larger view, but the code is used is not working. Code,

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfImages = [userDefaults objectForKey:@"property_images"];
NSLog(@"GGG %@",arrayOfImages);


self.imagesData=[[NSArray alloc]init];
self.imagesData = arrayOfImages;

NSString *docsDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES) objectAtIndex:0];


for (UIImageView *imageViewToSave in self.imagesData){


    NSInteger tag = imageViewToSave.tag;
    UIImage *image = imageViewToSave.image;
    NSString *imageName = [NSString stringWithFormat:@"Image%li.png",(long)tag];

    NSString *imagePath = [docsDir stringByAppendingPathComponent:imageName];

    // Log the image and path being saved.  If either of these are nil, nothing will be written.
    NSLog(@"Saving %@ to %@", image, imagePath);

    [UIImagePNGRepresentation(image) writeToFile:imagePath atomically:NO];
}
NSLog(@"Save Button Pressed");

This is how we can show image on tap gesture,

 NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSArray *arrayOfImages = [userDefaults objectForKey:@"property_images"];
NSLog(@"GGG %@",arrayOfImages);

self.imagesData=[[NSArray alloc]init];
self.imagesData = arrayOfImages;
NSLog(@"Image Array is %@",_imagesData);
//dispatch_group_t group = dispatch_group_create();
for(int i=0; i<self.imagesData.count;i++){

    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    [imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i]]
placeholderImage:[UIImage imageNamed:@"launch.png"]];

    // imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
    tap.numberOfTapsRequired = 1;
    imageView.tag = i;
    NSLog(@"KK %ld",imageView.tag);
    [imageView setUserInteractionEnabled:YES];
    [imageView addGestureRecognizer:tap];

    [self.scrollView addSubview:imageView];

   // dispatch_group_enter(group);

    [imageView sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:i]] placeholderImage:nil options:SDWebImageHighPriority completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        //dispatch_group_leave(group);
    }];

    _fullImage.hidden=NO;
    _fullView.hidden=NO;
}

The method for expand is this,

 -(void)expandImage:(UITapGestureRecognizer*)recogniser
{
    //  _fullImage.image = [UIImage imageNamed:[self.imagesData objectAtIndex:recogniser.view.tag]];




    [_fullImage sd_setImageWithURL:[NSURL URLWithString:[self.imagesData objectAtIndex:recogniser.view.tag]]
              placeholderImage:[UIImage imageNamed:@"launch.png"]];
    NSLog(@"RRR %@",self.imagesData);
    NSLog(@"TTT %ld",recogniser.view.tag);

    }

You can use this function:

UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                               id completionTarget, 
                               SEL completionSelector, 
                               void *contextInfo);

completionTarget , completionSelector and contextInfo all notified when the UIImage is done saving if not use this all then you can pass nil.

like.

UIImageWriteToSavedPhotosAlbum(your UIImage, nil, nil, nil);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM