andreagiavatto / AGPhotoBrowser

A photo browser for iOS inspired by Facebook.
Apache License 2.0
447 stars 85 forks source link

Add default support for setting images by URL via AFN. #28

Closed mikrobi closed 10 years ago

andreagiavatto commented 10 years ago

Thanks for the pull request @mikrobi but as i explained here https://github.com/andreagiavatto/AGPhotoBrowser/issues/6#issuecomment-24862409 the purpose of this component is to be easy to add/remove without forcing additional dependencies.

I understand the need to load image asynchronously and i decided to make the life easier for developers by introducing the possibility to add your custom cell.

The following is a code example that i will introduce in the demo for the next release, but it's already usable with the current one.

Create your own cell by inheriting from AGPhotoBrowserCell or implementing the AGPhotoBrowserCellProtocol (in the latter, you will have to handle the layout code yourself). Since you are probably already using AFNetworking (or maybe not) or any other 3rd party library to fetch remote content you can reuse that library without modifying the photo browser. In this way your code will be resilient to future updates to my library and you can easily replace AFNetworking or the 3rd party library if you have to. Personally, i love to use SDWebImage:

#import "AGPhotoBrowserCell.h"

@interface MyPhotoBrowserCell : AGPhotoBrowserCell
- (void)setCellImageWithURL:(NSURL *)url;
@end
#import "WAMPhotoBrowserCell.h"
#import "SDWebImageManager.h"

@implementation MyPhotoBrowserCell
- (void)setCellImageWithURL:(NSURL *)url
{    
    SDWebImageManager *manager = [SDWebImageManager sharedManager];
    [manager downloadWithURL:url
                     options:0
                    progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                    }
                   completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished) {
                       if (image) {
                           [self setCellImage:image];
                       }
    }];
}
@end

In the AGPhotoBrowserDataSource class, implement - (UITableViewCell<AGPhotoBrowserCellProtocol> *)cellForBrowser:(AGPhotoBrowserView *)browser withReuseIdentifier:(NSString *)reuseIdentifier to return MyPhotoBrowserCell.

rashidasgari commented 10 years ago

The problem with the asynchronous load is that the browser messes up the view and returns wrong index for the photos. This results in displaying wrong images.