【问题标题】:Image from URL with .zip ending图片来自以 .zip 结尾的 URL
【发布时间】:2021-08-24 01:21:35
【问题描述】:

我得到了一个需要从中提取图像的 URL。图片是 PNG,但 URL 如下所示:

http://something.../something.zip

如果我在 Safari 中打开此 URL,它会自动下载 PNG。但是,当我尝试加载程序时,我得到一个 nil UIImage 对象。

我的代码:

- (void)fetchImageFromURL: (NSString *)urlString {
    NSURL *url        = [NSURL URLWithString:urlString];
    
    NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
        if (error) {
            NSLog(@"%@", error.localizedDescription);
            
        } else {
            if (data) {
                UIImage *image = [UIImage imageWithData: data];
                if (image) {
                    [self.randomImageImageView setImage: image];
                } else {
                    NSLog(@"Image is nil.");
                }
            } else {
                NSLog(@"Could not retrieve data.");
            }
        }
    }];
    [task resume];
}

【问题讨论】:

    标签: ios objective-c nsdata nsurlsession nsurl


    【解决方案1】:

    这是因为 Safari 会自动解压缩小于特定大小的 zip 文件。这意味着您需要先以编程方式解压缩文件,然后从那里找到您的图像。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-22
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 2019-07-06
    • 2016-08-05
    • 1970-01-01
    相关资源
    最近更新 更多