我正在使用iCarousel
自定义控件来显示使用JSON数据的网页图片。
以下是我在iCarousel
在ViewDidLoad中加载JSON数据
JSONLoader *jsonLoader = [[JSONLoader alloc]init];
self.items = [[NSMutableArray alloc]init];
[self.items removeAllObjects];
self.items = (NSMutableArray *) [jsonLoader loadJSONDataFromURL:[NSURL URLWithString:@"https://public-api.wordpress.com/rest/v1/sites/www.myWebsite.com/posts?category=blog&page=1"]];
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(AsyncImageView *)view
{
MMSPLoader *mmObject = [self.items objectAtIndex:index];
view = [[AsyncImageView alloc]initWithFrame:CGRectMake(0, 0, 250.0f, 250.0f)];
view.layer.borderColor = [UIColor whiteColor].CGColor;
view.layer.borderWidth = 0.3f;
view.image=[UIImage imageNamed:@"page.png"];
view.imageURL = [NSURL URLWithString:[mmObject featureImageUrl]];
return view;
}
可以正确显示图像。我的情况是,当我点击该图像时,我想在FULL SCREEN中显示该图像。所以我使用了GGFullScreenImageViewController
。
然而,当我点击该图片以显示全屏时,我检索了图片网址并显示在GGFullScreenImageViewController
中。没关系,但我不想从该网址中检索,因为它再次从网上下载图片并放慢显示。
在我的想法中,我在点击iCarousel中的图像时保存了该图像并在GGFullScreenImageViewController
中显示。
所以我不需要再次下载图像了。
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
dispatch_queue_t myqueue = dispatch_queue_create("com.i.longrunningfunctionMain", NULL);
dispatch_async(myqueue, ^{
UIApplication *apps = [UIApplication sharedApplication];
apps.networkActivityIndicatorVisible = YES;
MMLoader *mmObject = [self.items objectAtIndex:index];
NSData *data = [[NSData alloc]initWithContentsOfURL:[NSURL URLWithString:mmObject.featureImageUrl]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageWithData:data]];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
dispatch_async(dispatch_get_main_queue(), ^{
apps.networkActivityIndicatorVisible = NO;
[self presentViewController:vc animated:YES completion:nil];
});
});
NSLog(@"%i",index);
}
我应该保存到本地文件还是有其他好主意?
答案 0 :(得分:1)
您最初下载时确实应该使用库来保存图像。 AsyncImageView
不一定是最佳选择,因为它只是在内存中缓存。
那就是说,此刻你可以从视图中获取图像。这并不理想,你应该把它保存到磁盘 - 只是提前而不是更晚。或许,请看SDWebImage
。
要从视图中获取图像(在浏览器中键入以便验证语法和API使用情况......):
- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
AsyncImageView *view = (AsyncImageView *)[carousel itemViewAtIndex:index];
UIImageView *imageView = [[UIImageView alloc] initWithImage:view.image];
GGFullscreenImageViewController *vc = [[GGFullscreenImageViewController alloc] init];
vc.liftedImageView = imageView;
[self presentViewController:vc animated:YES completion:nil];
}