iphone - 尝试显示图像数组,代码将 iPhone 返回到主屏幕

标签 iphone objective-c arrays image ios

这是问题代码:

代码用于显示一组图像,然后可以滚动这些图像。

- (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 GraphicsServices
0x017211c4 GSEventRunModal + 217 25 GraphicsServices
0x01721289 GSEventRun + 115 26 UIKit 0x002c8c93 UIApplicationMain + 1160 27 ALPHA
0x000026a8 main + 102 28 ALPHA
0x00002639 start + 53 )

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

再次感谢,

jack

最佳答案

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

这是你的问题。你有一个 NSString 和一个 UIImage 命名相同的东西。当您将 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 = ... 行,因为它是在括号中创建的。

希望这对您有所帮助!

关于iphone - 尝试显示图像数组,代码将 iPhone 返回到主屏幕,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5924111/

相关文章:

Objective-C 单例和 LLVM/clang 泄漏警告

iphone - 如何在AVPlayer播放时开始播放下一首歌曲或识别歌曲已结束?

iphone - Xcode 4.4,IOS 5.1模拟器。推特框架问题

iphone - 如何使用webkit的新功能管理phonegap部署

iphone - 通过 iOS 应用程序发布到用户墙时的隐私设置

objective-c - 在 Objective-C 中获取对象的属性数组

arrays - 由有界数据类型索引的 Repa 数组?

c - 字符串数组实现返回段错误

c - 输入退出条件后陷入死循环。

javascript - 想从服务器端清除 UIWebView 缓存