I am using iCarousel custom control to show image from web that consumed with JSON data.

Here is my codes to show image in iCarousel

to Load JSON Data in ViewDidLoad

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

enter image description here

That can show image correctly. My case is when i tap on that image , i want to show that image in FULL SCREEN. So i used GGFullScreenImageViewController.

However when i tap on that Image to show FULL SCREEN , i retrieved Image URL and show in GGFullScreenImageViewController. It's fine but , i don't want to retrieve from that URL because it downloading image from web again and slowing to show.

In my idea , i saved that image when tap on image in iCarousel and show it in GGFullScreenImageViewController.

So i don't need to download image again.

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

So should i save to local file or is there any others nice idea?

有帮助吗?

解决方案

Really you should use a library to save the image when you initially download it. AsyncImageView isn't necessarily the best choice as it just caches in memory.

That said, at the moment you can just get the image from the view. This isn't ideal, and you should save it to disk - just sooner rather than later. Look at, perhaps, SDWebImage for that.

To get the image from the view (typed in browser so verify syntax and API usage...):

- (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];
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top