【问题标题】:Trying to display array of images, code returns iphone to home screen试图显示图像数组,代码将 iphone 返回到主屏幕
【发布时间】:2011-05-07 21:28:57
【问题描述】:

问题代码如下:

代码是显示一组图像,然后可以滚动浏览。

- (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;
}

有什么想法吗?

[会话开始于 2011-05-07 22:46:47 +0100。] 2011-05-07 22:46:49.314 阿尔法 [1171:207] 加载01 2011-05-07 22:47:13.749 阿尔法[1171:207] 2 2011-05-07 22:47:13.751 阿尔法 [1171:207] 释放 01 2011-05-07 22:47:13.755 ALPHA[1171:207] load02 2011-05-07 22:47:19.791 阿尔法 [1171:207] 4 2011-05-07 22:47:19.792 ALPHA[1171:207] dealloc02 2011-05-07 22:47:19.795 阿尔法[1171:207]-[UIImage 长度]:无法识别的选择器发送到 实例 0x4b20f60 2011-05-07 22:47:19.797 阿尔法 [1171:207] * 由于未捕获而终止应用程序 例外 'NSInvalidArgumentException',原因: '-[UIImage 长度]: 无法识别 选择器发送到实例 0x4b20f60' * 首次抛出时调用堆栈:( 0 CoreFoundation
0x00dca5a9 异常预处理 + 185 1 libobjc.A.dylib
0x00f1e313 objc_exception_throw + 44 2 核心基础
0x00dcc0bb -[NSObject(NSObject) 不识别选择器:] + 187 3
核心基金会
0x00d3b966 __转发
+ 966 4
核心基金会
0x00d3b522 _CF_forwarding_prep_0 + 50 5 UIKit
0x003e3568 _UIImageAtPath + 36 6
阿尔法
0x000037fd -[PhotosView viewDidLoad] + 597 7 UIKit
0x0036a089 -[UIViewController 视图] + 179 8 阿尔法
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 发送操作:到:forEvent:] + 67 13 UIKit
0x0034cc2b -[UIControl(内部) _sendActionsForEvents:withEvent:] + 527 14 UIKit
0x0034b7d8 -[UIControl touchesEnded:withEvent:] + 458 15 UIKit
0x002deded -[UIWindow _sendTouchesForEvent:] + 567 16 UIKit
0x002bfc37 -[UIApplication 发送事件:] + 447 17 UIKit 0x002c4f2e _UIApplicationHandleEvent + 7576 18 图形服务
0x01722992 PurpleEventCallback + 1550 19 核心基金会
0x00dab944 CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION + 52 20 核心基础 0x00d0bcf7 __CFRunLoopDoSource1 + 215 21 核心基金会
0x00d08f83 __CFRunLoopRun + 979 22 核心基金会
0x00d08840 CFRunLoopRunSpecific + 208 23 核心基金会
0x00d08761 CFRunLoopRunInMode + 97 24 图形服务
0x017211c4 GSEventRunModal + 217 25 图形服务
0x01721289 GSEventRun + 115 26 UIKit 0x002c8c93 UIApplicationMain + 1160 27 阿尔法
0x000026a8 主 + 102 28 ALPHA
0x00002639 开始 + 53 )

在抛出 'NSException' 实例后调用终止

再次感谢,

杰克

【问题讨论】:

  • 它崩溃了,对吧?请给我们控制台输出和堆栈跟踪。
  • 另外,如果你能单步执行它并告诉我们哪条线导致崩溃,这将有很大帮助。
  • 看起来下面有一些很好的解释。如果它有效,您应该接受答案。如果没有,请提供更多信息。

标签: iphone objective-c arrays image ios


【解决方案1】:
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 = ... 行,因为它是在括号中创建的。

希望这会有所帮助!

【讨论】:

    【解决方案2】:

    photos 中,您已经存储了 UIImage 对象。为什么你尝试循环循环克里特岛UIImage 来自UIImage

    UIImage *image = [UIImage imageNamed:[photos objectAtIndex:i]];
    

    你应该这样做:

    UIImage *image = [photos objectAtIndex:i];
    

    【讨论】:

    • 这似乎是问题所在,可能 +imageNamed 方法想要获取字符串的长度,然后应用程序崩溃,因为 UIImage 不响应 -length。
    • 你是对的。当我们可以稍微了解框架方法的工作原理时,我喜欢这种情况。
    【解决方案3】:

    photos 是一个 UIImages 的 NSArray 是的,你将它枚举为字符串?

    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];
        }
    

    【讨论】:

    • 谢谢,改了,但还是崩溃:/
    • 其实没关系(在某些情况下只会出现警告)。在 Objective-c 中,所有类型都将在运行时检查。此外,for (id image in photos) 的工作速度稍快。这是 WWDC'2010 的一场表演讲座。
    【解决方案4】:
    for ( UIImage *img in photos ){
    
    UIImageView *imageView = [[UIImageView alloc] initWithImage:img];
    
    ...... // write something
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-19
      • 2012-03-18
      相关资源
      最近更新 更多