【问题标题】:How do I check what URL an image has been set with?如何检查图像设置的 URL?
【发布时间】:2020-06-05 15:46:40
【问题描述】:

我使用url 设置image,如下所示:

[self.imageView setMyImageWithURL:[NSURL URLWithString:[trip.destination getHeaderImageUrl] ]];
- (void)setMYImageWithURL:(NSURL *)url {
    [self setBackground];

    if(url.absoluteString.length >0){
        __block UIImageView *safeSelf = self;
        UIImage *placeholderImage = [UIImage imageNamed:[NSString stringWithFormat:@"placeholderpic_%@.png", NSLocalizedString(@"LanguageCode" , @"")]];
        [self setImageWithURLRequest:[NSURLRequest requestWithURL:url]
                    placeholderImage:placeholderImage
                             success:nil
                             failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error){
                                 safeSelf.image = [UIImage imageNamed:[NSString stringWithFormat:@"placeholderpic_%@_missing.png", NSLocalizedString(@"LanguageCode" , @"")]];
                             }
         ];
    }else{
        self.image = [self missingImage];
    }
}

现在,有没有办法在设置后检查imageView url。比如,NSLog(@"%@", self.imageView.usingURL)。我想使用条件检查它是否与另一个 url 相同。像这样的:

if(self.imageView.usingURL == [NSURL URLWithString:[trip.destination getHeaderImageUrl]])

【问题讨论】:

  • 无法从 UIImage 获取 URL 信息。但是这个answer 也可以解决您的问题。您可以发送标识符或使用 URL 的 absoluteString 值作为 UIImage 中的标识符,这将使您能够识别已加载图像的 url。
  • setImageWithURLRequest:placeholderImage:success:failure:: 你用的是什么库?图片上可能设置了一个值。

标签: ios objective-c url uiimage


【解决方案1】:

使用该类别在 UIImageView 中添加属性

@interface UIImageView (URLImageView)
    @property NSURL* usingURL;
@end

@implementation UIImageView (URLImageView)]
@end

现在当图像下载并设置为图像视图时,将 url 分配给图像视图,如

self.imageView.usingURL = yourURL

现在您可以访问此网址进行比较。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多