图像未加载图像
Image not loading for image
我只想为 UIButton
设置图像并为其创建一个 IBOutlet
。我在它上面有一个加载器,当加载图像时,加载器将停止并为该按钮分配一个新图像。调用代码时,占位符图像仍然存在,url 图像未加载
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
如果您正在为 SDWebImageCache
使用完成块,请在完成块内设置图像
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
_Image1.image = image;
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
在主线程中更新图像尝试这样的事情
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
dispatch_sync(dispatch_get_main_queue(), ^{
_Image1.image = image;
});
}];
您正在使用完成块从 url 下载图像。所以你需要按照下面的方式去做。
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_Image1 setImage:btnImage forState:UIControlStateNormal];//Don't forgot to check for error if error is nil or image is not nil then only set image
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
从 NSUrlRequest
和 NSUrl URLWithString
"your url"
得到 NSData
并将NSData
转换为UIImage
并设置背景图片。
首先获取图像url并检查图像url长度是否大于零/不null
。并确保图像 url 是否正常工作。在 Browser
上点击 url
并检查您是否获得了正确的图像。
SDWebImage
库正在更新按钮图像,但这在 Main Thread
上没有发生。这可能是不更新 UIButton
的 backgroundImage
的原因。
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
forState:(UIControlState)state
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
completed:(nullable SDExternalCompletionBlock)completedBlock {
if (!url) {
[self.imageURLStorage removeObjectForKey:@(state)];
return;
}
NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
self.imageURLStorage[@(state)] = url;
__weak typeof(self)weakSelf = self;
[self sd_internalSetImageWithURL:url
placeholderImage:placeholder
options:options
operationKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]
setImageBlock:^(UIImage *image, NSData *imageData) {
[weakSelf setBackgroundImage:image forState:state];
}
progress:nil
completed:completedBlock];
}
停止 loader
动画并在 main thread
上设置 UIButton
背景图像。
NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
if (imageURL.length > 0) {
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
dispatch_async(dispatch_get_main_queue(), ^{
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
[_Image1 setBackgroundImage:image forState:UIControlStateNormal];
});
}];
}
我只想为 UIButton
设置图像并为其创建一个 IBOutlet
。我在它上面有一个加载器,当加载图像时,加载器将停止并为该按钮分配一个新图像。调用代码时,占位符图像仍然存在,url 图像未加载
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
如果您正在为 SDWebImageCache
使用完成块,请在完成块内设置图像
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
_Image1.image = image;
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
在主线程中更新图像尝试这样的事情
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
dispatch_sync(dispatch_get_main_queue(), ^{
_Image1.image = image;
});
}];
您正在使用完成块从 url 下载图像。所以你需要按照下面的方式去做。
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:[arrayGallery valueForKey:@"link"][0]] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
[_Image1 setImage:btnImage forState:UIControlStateNormal];//Don't forgot to check for error if error is nil or image is not nil then only set image
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
}];
从 NSUrlRequest
和 NSUrl URLWithString
"your url"
NSData
并将NSData
转换为UIImage
并设置背景图片。
首先获取图像url并检查图像url长度是否大于零/不null
。并确保图像 url 是否正常工作。在 Browser
上点击 url
并检查您是否获得了正确的图像。
SDWebImage
库正在更新按钮图像,但这在 Main Thread
上没有发生。这可能是不更新 UIButton
的 backgroundImage
的原因。
- (void)sd_setBackgroundImageWithURL:(nullable NSURL *)url
forState:(UIControlState)state
placeholderImage:(nullable UIImage *)placeholder
options:(SDWebImageOptions)options
completed:(nullable SDExternalCompletionBlock)completedBlock {
if (!url) {
[self.imageURLStorage removeObjectForKey:@(state)];
return;
}
NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
self.imageURLStorage[@(state)] = url;
__weak typeof(self)weakSelf = self;
[self sd_internalSetImageWithURL:url
placeholderImage:placeholder
options:options
operationKey:[NSString stringWithFormat:@"UIButtonBackgroundImageOperation%@", @(state)]
setImageBlock:^(UIImage *image, NSData *imageData) {
[weakSelf setBackgroundImage:image forState:state];
}
progress:nil
completed:completedBlock];
}
停止 loader
动画并在 main thread
上设置 UIButton
背景图像。
NSString *imageURL = [arrayGallery valueForKey:@"link"][0];
if (imageURL.length > 0) {
[_Image1 sd_setBackgroundImageWithURL:[NSURL URLWithString:imageURL] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"blurred-background"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
dispatch_async(dispatch_get_main_queue(), ^{
[_ImageLoader1 stopAnimating];
[_ImageLoader1 removeFromSuperview];
[_Image1 setBackgroundImage:image forState:UIControlStateNormal];
});
}];
}