您的位置:首页 > 产品设计 > UI/UE

uicollection使用sdwebimage加载图片不出来

2016-12-31 03:15 246 查看
我照着网上的例子,准备用uicollectionview展示照片,而照片的下载使用了SDwebimage,但是显示出来就是空白,求解。直接上代码了,layout布局没有错的
#import <SDWebImage/UIImageView+WebCache.h>

@interface PhotoWallViewController ()

{

    NSMutableArray *_objects;

}

@end

@implementation PhotoWallViewController

@synthesize _datasource;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil

{

    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self) {

        // Custom initialization

    }   

    return self;

}

- (void)viewDidLoad

{

    [super viewDidLoad];

    

    // Do any additional setup after loading the view from its nib.

    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"mycell"];

    self._datasource = [[[NSMutableArray alloc]init]autorelease];

    

    _objects = [NSArray arrayWithObjects:

                @"http://www.raiseakitten.com/wp-content/uploads/2012/03/kitten.jpg",@"http://imagecache6.allposters.com/LRG/21/2144/C8BCD00Z.jpg",

                @"http://www.raiseakitten.com/wp-content/uploads/2012/03/kitten.jpg",@"http://imagecache6.allposters.com/LRG/21/2144/C8BCD00Z.jpg",

                @"http://static2.dmcdn.net/static/video/893/348/51843398:jpeg_preview_small.jpg?20121105214157",

                @"http://static2.dmcdn.net/static/video/351/748/51847153:jpeg_preview_small.jpg?20121105214106",

                @"http://www.raiseakitten.com/wp-content/uploads/2012/03/kitten.jpg",@"http://imagecache6.allposters.com/LRG/21/2144/C8BCD00Z.jpg",

                @"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014",

                @"http://static2.dmcdn.net/static/video/720/448/51844027:jpeg_preview_small.jpg?20121105214248",

                @"http://static2.dmcdn.net/static/video/895/048/51840598:jpeg_preview_small.jpg?20121105214234",

                @"http://static2.dmcdn.net/static/video/893/348/51843398:jpeg_preview_small.jpg?20121105214157",

                @"http://static2.dmcdn.net/static/video/351/748/51847153:jpeg_preview_small.jpg?20121105214106",

                @"http://static2.dmcdn.net/static/video/364/648/51846463:jpeg_preview_small.jpg?20121105215005",

                @"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014",

                @"http://static2.dmcdn.net/static/video/720/448/51844027:jpeg_preview_small.jpg?20121105214248",

                @"http://static2.dmcdn.net/static/video/895/048/51840598:jpeg_preview_small.jpg?20121105214234",

                @"http://static2.dmcdn.net/static/video/893/348/51843398:jpeg_preview_small.jpg?20121105214157",

                @"http://static2.dmcdn.net/static/video/351/748/51847153:jpeg_preview_small.jpg?20121105214106",

                @"http://static2.dmcdn.net/static/video/364/648/51846463:jpeg_preview_small.jpg?20121105215005",

                @"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014",

                @"http://static2.dmcdn.net/static/video/364/648/51846463:jpeg_preview_small.jpg?20121105215005",

                @"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014",

                nil];

    self.collectionView.backgroundColor = [UIColor whiteColor];

self.collectionView.allowsSelection = YES;

    self.collectionView.frame = self.view.bounds;

    self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);

    

    [((WaterFlowLayout*)self.collectionView.collectionViewLayout) setFlowdatasource:self];

    [((WaterFlowLayout*)self.collectionView.collectionViewLayout) setFlowdelegate:self];

    

    //[SDWebImageManager.sharedManager.imageDownloader setValue:@"SDWebImage Demo" forHTTPHeaderField:@"AppName"];

    

   // SDWebImageManager.sharedManager.imageDownloader.executionOrder = SDWebImageDownloaderLIFOExecutionOrder;

}

- (void)didReceiveMemoryWarning

{

    [super didReceiveMemoryWarning];

}

- (NSInteger) numberOfSectionsInCollectionView:(UICollectionView *)collectionView

{

    return 1;

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section

{

    return 20;

}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *CellIdentifier = @"mycell";

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];

    if (cell == nil) {

        cell = [[UICollectionViewCell alloc]init];

    }

    

    //UIImageView *imageview = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"0.JPG"]];

    //UIImageView *imageview = [[UIImageView alloc ]init];

   // cell.imageview.image = [UIImage imageNamed:@"login"];

    //[cell.contentView addSubview:image ];

    //[imageview release];

    

    NSURL *URL = [NSURL URLWithString:@"http://static2.dmcdn.net/static/video/269/938/51839962:jpeg_preview_small.jpg?20121105214014"];

    UIImageView *imageview = [[UIImageView alloc]init];

    imageview.image = [UIImage imageNamed:@"placeholder"];

    [imageview setImageWithURL:URL placeholderImage:[UIImage imageNamed:@"placeholder"]options:indexPath.row == 0 ? SDWebImageRefreshCached : 0];

    [cell.contentView addSubview:imageview];

    

    return cell;

}

#pragma mark - UICollectionViewDelegate methods

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath

{

    NSLog(@"%d selected", indexPath.item);

}

#pragma mark-  UICollecitonViewDelegateWaterFlowLayout

- (CGFloat)flowLayout:(WaterFlowLayout *)flowView heightForRowAtIndex:(int)index

{

    float height = 0;

switch (index  % 5) {

case 0:

height = 127;

break;

case 1:

height = 100;

break;

case 2:

height = 87;

break;

case 3:

height = 114;

break;

case 4:

height = 140;

break;

case 5:

height = 158;

break;

default:

break;

}

return height;

}

#pragma mark- UICollectionViewDatasourceFlowLayout

- (NSInteger)numberOfColumnsInFlowLayout:(WaterFlowLayout*)flowlayout

{

    return 2;

}

@end 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: