简体   繁体   English

如何在IOS中将URL图像保存到图库

[英]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. 来自服务器的图像采用url形式。 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. completionTarget,completionSelectorcontextInfo当UIImage的储存完成后,如果没有使用这一切,那么你可以通过零的所有通知。

like. 喜欢。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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