下载并保存可以为空白的图像

发布于 2024-10-01 05:08:12 字数 902 浏览 4 评论 0原文

我正在使用 NSURLConnection 从网络下载并保存雷达图像(.gif 格式)。在 connectionDidFinishLoading: 方法中,我使用 [UIImage imageWithData:] 创建图像。

- (void)connectionDidFinishLoading:(NSURLConnection *)connection  
{  
    // Problem is here. mutableData declared earlier.
    UIImage *baseImage = [UIImage imageWithData:mutableData];  

    if (baseImage)  
    {
        // do stuff with image
    }

    [mutableData release];
}

问题是我下载的图像有时是空白的并且背景清晰。这不是图像中的错误(有时雷达上没有任何东西可以返回)。但是当发生这种情况时,我在控制台中收到此错误。当图像的像素数量非常少(1 或 2)时,也会出现此错误。

Error: ImageIO: malformed GIF frame#0 (600 x 550)

发生错误时,创建的图像为零。 600x550 是我正在下载的图像的大小(以 px 为单位)。所以返回的图像为零,这使得下载失败。

值得注意的是,baseImage 在两种情况下可能会失败,一种是上面提到的,另一种是当我下载服务器上列出的但实际上尚不存在的图像时。在第二种情况下,mutableData 包含错误字符串。我仍然需要第二种情况失败,这样我就知道图像没有成功下载。

在这种情况下,我需要做的实际上是创建空白图像并保存它,以便用户能够看到下载是否正确执行。

有什么想法吗?

谢谢, 罗斯

I am downloading and saving a radar image (in .gif format) from with web using an NSURLConnection. In the connectionDidFinishLoading: method, I create an image using [UIImage imageWithData:].

- (void)connectionDidFinishLoading:(NSURLConnection *)connection  
{  
    // Problem is here. mutableData declared earlier.
    UIImage *baseImage = [UIImage imageWithData:mutableData];  

    if (baseImage)  
    {
        // do stuff with image
    }

    [mutableData release];
}

The problem is that the images I am downloading are sometime blank and have clear background. This is NOT an error in the image (sometimes there is nothing on the radar to return). But when this happens, I am getting this error in the console. The error can also occur when the image has a very small number of pixels (1 or 2).

Error: ImageIO: malformed GIF frame#0 (600 x 550)

When the error occurs, the image that is created is nil. The 600x550 is the size in px of the image I am downloading. So the returned image in nil, which makes it seem as the download has failed.

It is worth noting that baseImage can fail in 2 cases, the one mentioned above or when I download an image that is listed on the server that is not actually there yet. In the second case, mutableData contains an error string. I still need the 2nd case to fail so I know that an image was not successfully downloaded.

What I need to do in this case is actually create the blank image and save it so the user will be able to see that the download performed correctly.

Any ideas?

Thanks,
Ross

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

那支青花 2024-10-08 05:08:12

使用此代码:

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://files.me.com/ross.kimes/6cuagt"]];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"%@", image);

恐怕我在控制台和有效的 UIImage 对象中没有收到错误:(


我想您可以检测到 string= 的存在 - 即 NSSTring stringWithData:encoding: - 如果它是有效的字符串和图像是 nil 您可能会认为这是一条错误消息?

Using this code :

    NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://files.me.com/ross.kimes/6cuagt"]];
UIImage *image = [UIImage imageWithData:data];
NSLog(@"%@", image);

I'm afraid I get no errors in the console and a valid UIImage object :(


I suppose that you could detect the presence of a string= - i.e. NSSTring stringWithData:encoding: - if it's a valid string and the image is nil you could probably assume that it's an error message?

别理我 2024-10-08 05:08:12

也许你可以尝试这样的事情:

@try{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://files.me.com/ross.kimes/6cuagt"]];
UIImage *image = [UIImage imageWithData:data];
}@catch (NSException *ex) {
//Something goes wrong, create the image
}

Maybe you can ry something like:

@try{
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://files.me.com/ross.kimes/6cuagt"]];
UIImage *image = [UIImage imageWithData:data];
}@catch (NSException *ex) {
//Something goes wrong, create the image
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文