尝试显示图像数组,代码导致iPhone返回主屏幕

3

以下是有问题的代码:

这段代码用于显示一个图片数组,用户可以通过滚动来查看所有图片。

- (void)viewDidLoad {
    [super viewDidLoad];

    NSArray *photos = [[NSArray arrayWithObjects:   
                        [UIImage imageNamed:@"Event.png"],
                        [UIImage imageNamed:@"Logo.png"],
                        [UIImage imageNamed:@"default.png"],
                        [UIImage imageNamed:@"LittleLogoImage.png"],
                        nil] retain];      // TODO – fill with your photos

    // note that the view contains a UIScrollView in aScrollView

    int i=0;
    int width = 0;
    int height = 0;
    for ( NSString *image in photos )
    {
        UIImage *image = [UIImage imageNamed:[photos objectAtIndex:i]];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;

        imageView.frame = CGRectMake( imageView.frame.size.width * i++, 0, imageView.frame.size.width, imageView.frame.size.height);

        [aScrollView addSubview:imageView];
        width = imageView.frame.size.width;
        height = imageView.frame.size.height;

        [imageView release];
    }
    aScrollView.contentSize = CGSizeMake(width*i, height);
    aScrollView.delegate = self;
}

有什么想法吗?

[Session started at 2011-05-07 22:46:47 +0100.] 2011-05-07 22:46:49.314 ALPHA[1171:207] load01 2011-05-07 22:47:13.749 ALPHA[1171:207] 2 2011-05-07 22:47:13.751 ALPHA[1171:207] dealloc01 2011-05-07 22:47:13.755 ALPHA[1171:207] load02 2011-05-07 22:47:19.791 ALPHA[1171:207] 4 2011-05-07 22:47:19.792 ALPHA[1171:207] dealloc02 2011-05-07 22:47:19.795 ALPHA[1171:207] -[UIImage length]: unrecognized selector sent to instance 0x4b20f60 2011-05-07 22:47:19.797 ALPHA[1171:207] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x4b20f60' * Call stack at first throw: ( 0 CoreFoundation
0x00dca5a9 exceptionPreprocess + 185 1 libobjc.A.dylib
0x00f1e313 objc_exception_throw + 44 2 CoreFoundation
0x00dcc0bb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187 3
CoreFoundation
0x00d3b966 __forwarding
+ 966 4
CoreFoundation
0x00d3b522 _CF_forwarding_prep_0 + 50 5 UIKit
0x003e3568 _UIImageAtPath + 36 6
ALPHA
0x000037fd -[PhotosView viewDidLoad] + 597 7 UIKit
0x0036a089 -[UIViewController view] + 179 8 ALPHA
0x00002af3 -[ALPHAViewController displayView:] + 500 9 ALPHA
0x00002707 -[ALPHAAppDelegate displayView:] + 60 10 ALPHA
0x00002cdd -[View2 viewPhotos:] + 99 11 UIKit
0x002ba4fd -[UIApplication sendAction:to:from:forEvent:] + 119 12 UIKit
0x0034a799 -[UIControl sendAction:to:forEvent:] + 67 13 UIKit
0x0034cc2b -[UIControl(Internal) _sendActionsForEvents:withEvent:] + 527 14 UIKit
0x0034b7d8 -[UIControl touchesEnded:withEvent:] + 458 15 UIKit
0x002deded -[UIWindow _sendTouchesForEvent:] + 567 16 UIKit
0x002bfc37 -[UIApplication sendEvent:] + 447 17 UIKit 0x002c4f2e _UIApplicationHandleEvent + 7576 18 GraphicsServices
0x01722992 PurpleEventCallback + 1550 19 CoreFoundation
0x00dab944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 20 CoreFoundation 0x00d0bcf7 __CFRunLoopDoSource1 + 215 21 CoreFoundation
0x00d08f83 __CFRunLoopRun + 979 22 CoreFoundation
0x00d08840 CFRunLoopRunSpecific + 208 23 CoreFoundation
0x00d08761 CFRunLoopRunInMode + 97 24 Graphics

抛出“NSException”实例后终止调用

再次感谢,

杰克


它崩溃了,对吧?请给我们提供控制台输出和堆栈跟踪。 - Nick Weaver
1
另外,如果您可以逐行查看并告诉我们哪一行导致了崩溃,那将非常有帮助。 - Mikecito
看起来下面有几个不错的解释。如果它能正常工作,你应该接受一个答案。如果不能,请提供更多信息。 - Austin Taylor
4个回答

8
for ( NSString *image in photos )
{
    UIImage *image = [UIImage imageNamed:[photos objectAtIndex:i]];
    ...

这是你的问题。你有一个名字相同的NSStringUIImage。当你将NSString重新声明为UIImage时,会混淆forin循环。循环似乎仍然认为你有一个NSString,而实际上你有一个UIImage。所以我们的第一步是尝试用以下代码替换它:

for ( NSString *imageName in photos )
{
    UIImage *image = [UIImage imageNamed:imageName];
    ...

然而,那段代码也无法工作。幸运的是,你的images数组已经存储了UIImages,所以你根本不需要NSString!因此,你可以用以下代码替换循环的前三行:

for ( UIImage *image in photos )
{
    ...

就是这样!你不需要你的UIImage *image = ...这一行,因为它已经在括号中创建了。

希望这可以帮到你!


1
photos 中,您已经存储了 UIImage 对象。为什么要在循环中尝试从 UIImage 创建 UIImage
UIImage *image = [UIImage imageNamed:[photos objectAtIndex:i]];

你应该这样做:
UIImage *image = [photos objectAtIndex:i];

这似乎是问题所在,可能是+imageNamed方法想要获取字符串的长度,然后导致应用程序崩溃,因为UIImage不响应-length方法。 - Henri Normak
你说得对。我喜欢这种情况,因为我们可以稍微了解一下框架方法的工作原理。 - 5hrp

0
for ( UIImage *img in photos ){

UIImageView *imageView = [[UIImageView alloc] initWithImage:img];

...... // write something

}

0

photos是一个UIImage数组,你正在将它枚举为字符串吗?

for ( NSString *image in photos ) 

应该是:

for ( UIImage *image in photos )
    {
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        imageView.contentMode = UIViewContentModeScaleAspectFit;
        imageView.clipsToBounds = YES;

        imageView.frame = CGRectMake( imageView.frame.size.width * i++, 0, imageView.frame.size.width, imageView.frame.size.height);

        [aScrollView addSubview:imageView];
        width = imageView.frame.size.width;
        height = imageView.frame.size.height;

        [imageView release];
    }

谢谢,我已经改了,但它仍然崩溃 :/ - Jack Nutkins
实际上,这并不重要(只有在某些情况下才会出现警告)。在Objective-C中,所有类型都将在运行时进行检查。此外,for (id image in photos) 的速度略快。这是在WWDC'2010的性能讲座中提到的。 - 5hrp

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接