1、通过NSData加载图片:
[photoBtn setImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]] forState:UIControlStateNormal];
2、通过GCD加载:
[photoBtn setImage:[UIImage imageNamed:@"icon_Image_Placeholder"] forState:UIControlStateNormal]; //!< 加这句可以设置button占位图片
UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageUrl]]];
dispatch_async(dispatch_get_main_queue(), ^{
if(img) {
[photoBtnsetImage:imgforState:UIControlStateNormal];
}
});
});
3、通过SDWebImage加载,带缓存:
[photoBtn.imageView sd_setImageWithURL:[NSURL URLWithString:imageUrl] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
if(image) {
[photoBtn setImage:image forState:UIControlStateNormal];
}
}];
网友评论