EddyBorja / EBPhotoPages

A photo gallery for iOS with a modern feature set. Similar features as the Facebook photo browser.
MIT License
1.63k stars 189 forks source link

Refreshing image #9

Open serkanb opened 10 years ago

serkanb commented 10 years ago

Hello, First of all thanks for wonderful control. I have a problem with refreshing images. My app download images from web server with SDWebImage. It shows a placeholder image until the real one is received. The image is embedded in imageAtIndex method. The problem is the real image is not replaced with placeholder image.

- (void)photoPagesController:(EBPhotoPagesController *)controller
                imageAtIndex:(NSInteger)index
           completionHandler:(void (^)(UIImage *))handler
{
    dispatch_queue_t queue =     dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);     
    dispatch_async(queue, ^{
        MyPhoto *photo = self.photos[index];
        handler(photo.imageView.image);
    });
}

How can I refresh the image? Could you help me please?

EddyBorja commented 10 years ago

photo.imageView.image is holding a placeholder image until it downloads the real image from the server, which causes the placeholder to be displayed in the photo gallery instead of the real photo.

I don't know much about SDWebImage, but does it have some kind of way of notifying that it has finished downloading the image from the server?

If not, I'd recommend finding another way of downloading images from the server or modifying SDWebImage. Right now there's no support for refreshing photos in the gallery once you've passed in an image to display.

serkanb commented 10 years ago

Thanks Eddy, I used SDWebImages below method like you said.

    [self.manager cachedImageExistsForURL:photo.url completion:^(BOOL isInCache) { ... }

How can I show activity indicator until image is became ready?

EddyBorja commented 10 years ago

There used to be built in support for an activity indicator animating while waiting for content to appear, but due to a bug it's not working.

Not a very useful answer, but I just haven't had time to fix it with all my other work going on.

On Monday, July 21, 2014, serkanb notifications@github.com wrote:

Thanks Eddy, I used SDWebImages below method like you said.

[self.manager cachedImageExistsForURL:photo.url completion:^(BOOL isInCache) { ... }

How can I show activity indicator until image is became ready?

— Reply to this email directly or view it on GitHub https://github.com/EddyBorja/EBPhotoPages/issues/9#issuecomment-49659255 .

serkanb commented 10 years ago

Activity indicator started to run after I had made below changes in EBPhotoViewController and EBImageLoadOperation

in EBPhotoViewController I've made below chances:


    #pragma mark - (Operations)
    @implementation EBPhotoViewController (Operations)

    - (void)operationWillStartLoading:(NSOperation *)operation
    {
        NSAssert(self.activeOperations, @"Must have a pendingOperations set.");
        // I've added below if check
        if ([operation isKindOfClass:[EBImageLoadOperation class]]) {
            [self.activeOperations addObject:operation];
            [self showActivityIndicator];
        }
    }

    - (void)operationDidStopLoading:(NSOperation *)operation
    {
        NSAssert(self.activeOperations, @"Must have a pendingOperations set.");
      // I've removed below assert 
      // NSAssert([self.activeOperations containsObject:operation], @"PendingOperations did not contain given operation.");

        [self.activeOperations removeObject:operation];
     // I've removed below if 
     // if(self.activeOperations.count == 0){
        //I've added below if check
        if ([operation isKindOfClass:[EBImageLoadOperation class]]) {
               [self hideActivityIndicator];
        }
        //}
}

in EBImageLoadOperation I've made below changes


@implementation EBImageLoadOperation

@implementation EBPhotoPagesOperation

- (void)main
{
    @autoreleasepool {
        NSAssert(self.photoPagesController, @"Must have a photo pages controller assigned.");
        NSAssert(self.photoViewController, @"Must have a photo view controller to pass image to");
        NSAssert(self.dataSource,@"Must have a datasource to retrieve image from");

        [self setLoadingFinished:NO];
        [self operationWillStart];
        [self loadData];
        // I've removed below line.
 //       [self operationDidFinish];
    }
}

#pragma mark - Image Loading

@implementation EBImageLoadOperation
- (void)loadData
{

    if([self.dataSource respondsToSelector:@selector(photoPagesController:imageAtIndex:)]){
        [self performSelectorOnMainThread:@selector(loadImageOnMainThread)
                               withObject:nil
                            waitUntilDone:NO];

    } else if ([self.dataSource respondsToSelector:
                @selector(photoPagesController:imageAtIndex:completionHandler:)]){

        [self.dataSource photoPagesController:self.photoPagesController
                                 imageAtIndex:self.photoViewController.photoIndex
                            completionHandler:^(UIImage *image){

            [self.photoViewController performSelectorOnMainThread:@selector(setImage:)
                                                       withObject:image
                                                    waitUntilDone:YES];

           // I've added  below line 
           [self operationDidFinish];
        }];
    }
}