使用URL交换图像时崩溃

时间:2011-06-24 18:34:41

标签: iphone

@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   SlideShow [13654:207] 终止   应用程序由于未捕获的异常   'NSInvalidArgumentException',原因:   ' ** - [NSPlaceholderString   stringwithString:]:无法识别   选择器发送到实例0x49117e0'   2011-06-24 23:54:01.840   SlideShow [13654:207] Stack :(       42178640,       43336492,       42187355,       41649782,       41646578,       12567,       7791,       2906510,       2910543,       2936126,       2917623,       2949592,       51171708,       41457820,       41453736,       2908705)抛出'NSException'实例后调用终止

如果URL被图像替换,它就有效 任何人都可以帮助我 我是初学者,所以很难找到它

感谢

1 个答案:

答案 0 :(得分:0)

您正在尝试使用实例(在此处创建错误的实例)调用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,系统将在需要时调用它以最小化应用程序内存占用。