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

时间:2017-09-28 06:11:17

标签: ios objective-c image

我有来自服务器的一系列图像。我有一个按钮将其保存到图库。来自服务器的图像采用网址格式。现在我想下载从数组中选择的图像。我有一个按钮,然后在较大的视图中打开图像,但使用的代码不起作用。 代码,

 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");

这就是我们如何在点击手势上显示图像,

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

扩展的方法就是这个,

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

    }

1 个答案:

答案 0 :(得分:0)

您可以使用此功能:

UIImageWriteToSavedPhotosAlbum(UIImage *image, 
                               id completionTarget, 
                               SEL completionSelector, 
                               void *contextInfo);
completionTarget completionSelector contextInfo 都会在UIImage完成后通知保存,如果不使用此全部,则可以传递nil。

<强>等。

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