【问题标题】:Crash when using URL for swapping images使用 URL 交换图像时崩溃
【发布时间】:2011-06-24 18:34:40
【问题描述】:
@implementation SlideShowViewController
- (id)init
{
    NSString *temp = [NSString alloc];
    [temp stringwithString:@"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg"];
    temp=[(NSString *)CFURLCreateStringByAddingPercentEscapes(
                                                              nil,
                                                              (CFStringRef)temp,                     
                                                              NULL,
                                                              NULL,
                                                              kCFStringEncodingUTF8)
          autorelease];
    NSData *dato = [NSData alloc];
    dato=[NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
    if (self = [super initWithNibName:nil bundle:nil])
    {
        NSArray * images = [NSArray arrayWithObjects:[UIImage imageWithData:dato],[UIImage imageWithData:dato],  [UIImage imageWithData:dato], [UIImage imageWithData:dato], [UIImage imageWithData:dato], nil];

        self.view = [[[SlideShowView alloc] initWithImages:images] autorelease];
    }

    return self;
}

我使用以下代码从服务器加载图像并将其视为相册的图像

但是当代码运行时它会崩溃 控制台报错信息如下

2011-06-24 23:54:01.837 幻灯片[13654:207] * -[NSPlaceholderString stringwithString:]:无法识别 选择器发送到实例 0x49117e0 2011-06-24 23:54:01.839 幻灯片[13654:207] 终止 应用程序由于未捕获的异常 'NSInvalidArgumentException',原因: '** -[NSPlaceholderString stringwithString:]: 无法识别 选择器发送到实例 0x49117e0' 2011-06-24 23:54:01.840 幻灯片[13654:207] 堆栈:( 42178640, 43336492, 42187355, 41649782, 41646578, 12567, 7791, 2906510, 2910543, 2936126, 2917623, 2949592, 51171708, 41457820, 41453736, 2908705 ) 在抛出“NSException”实例后调用终止

如果 URL 被图像替换,它就可以工作 谁能帮助我 我是一个初学者,所以我很难找到它

谢谢

【问题讨论】:

    标签: iphone


    【解决方案1】:

    您正在尝试在此处调用带有实例(当时创建错误的实例)的 NSString 类方法

    NSString *temp = [NSString alloc];
        [temp stringwithString:@"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg"];
    

    改成

    NSString *temp = @"http://www.inetwallpaper.com/homescreenhero/sunsets/wall009.jpg";
    

    编辑:

    您做错了几件事,例如在某事上调用alloc,然后将它们设置为其他内容。 (*temp 和 *data)当你 alloc 时,它应该总是跟在 initinitXXXX 的调用之后。接下来,您甚至不需要那些 alloc 调用,因为您正在将指针设置为它正下方的行上的其他内容,这会导致内存泄漏。

    这就是你所需要的

    NSData *dato = [NSData dataWithContentsOfURL:[NSURL URLWithString:temp]];
    

    然后,您将创建一堆具有相同数据对象的图像。您在下载图像时也阻塞了调用线程,这应该稍后在 viewDidLoad 异步时间左右完成。

    视图控制器的init函数不是设置视图的地方。实现 loadView,系统会在需要时调用它,以最小化应用程序的内存占用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-14
      • 1970-01-01
      相关资源
      最近更新 更多