【问题标题】:UIImage from NSData works fine on iPad2 but not on iPad1NSData 的 UIImage 在 iPad2 上运行良好,但在 iPad1 上却不行
【发布时间】:2011-06-08 08:01:41
【问题描述】:

我刚刚遇到了一个非常奇怪的问题。 我目前正在开发一个应用程序,它以异步方式从远程主机加载图像,然后显示它们。

以下代码正在后台线程中执行,通过performSelectorInBackground 调用

 NSURL *url = [NSURL URLWithString:[ChannelThumbnailURI stringByReplacingOccurrencesOfString:@"{PersonName}" withString:person.Name]];  
    NSData *imageData = [NSData dataWithContentsOfURL:url];

    UIImage *thumb = [UIImage imageWithData:imageData]; 

现在有趣的是,我在不同的设备上得到了不同的结果(都使用相同的 iOS,4.3.3)

thumb 在我的旧 iPad 上为零,而在相同的上下文中,它在我的 iPad2 上是有效图像

我真的不知道这里可能出现什么问题。 感谢您的意见。

山姆

【问题讨论】:

  • 尝试通过检查 url 值/NSData 长度来确定问题来自何处进行调试,我敢打赌这是人为错误(例如由于缺少 person.Name 而导致的错误 url)

标签: ios ipad asynchronous uiimage nsdata


【解决方案1】:

我看不出为什么这会在 iPad2 上而不是在 iPad1 上运行的直接原因,请尝试添加更多调试代码以查看哪里出错了。

NSString* urlString = [ChannelThumbnailURI stringByReplacingOccurrencesOfString:@"{PersonName}" withString:person.Name];
if (urlString == nil) { ... }
NSURL *url = [NSURL URLWithString:urlString];
if (url == nil) { ... } 
NSError* error = nil; 
NSData *imageData = [[NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:&error];
if (error) {
    NSLog(@"%@", [error localizedDescription]);
    [error release];
}
UIImage *thumb = [UIImage imageWithData:imageData]; 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-22
    • 2018-04-10
    • 1970-01-01
    • 2019-02-11
    相关资源
    最近更新 更多