1.常用的button样式,图片在上,文字在下
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]
}
}]
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)