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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了试图显示图像数组,代码将iphone返回主屏幕相关的知识,希望对你有一定的参考价值。

这是问题代码:

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

- (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 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长度]:无法识别的选择器发送到实例0x4b20f60 2011- 05-07 22:47:19.797 ALPHA [1171:207] *由于未捕获的异常'NSInvalidArgumentException'终止应用程序,原因:' - [UIImage长度]:无法识别的选择器发送到实例0x4b20f60'*第一次调用堆栈:(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 Α 0x000037fd - [PhotosView viewDidLoad] + 597 7 UIKit 0x0036a089 - [UIViewController视图] + 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(内部)_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开始+ 53)

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

再次感谢,

插口

答案
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 = ...线,因为它是在括号中创建的。

希望这可以帮助!

另一答案

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

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

你应该这样做:

UIImage *image = [photos objectAtIndex:i];
另一答案

照片是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];
    }
另一答案
for ( UIImage *img in photos ){

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

...... // write something

}

以上是关于试图显示图像数组,代码将iphone返回主屏幕的主要内容,如果未能解决你的问题,请参考以下文章

你如何让图像在 iPhone 主屏幕上摇摆不定?

在 iPhone 上显示多个图像缩略图时 UI 失真

如何将iphone屏幕截图保存为jpeg图像?

相机屏幕仅显示空白黑色图像

初始化时iPhone X上显示的两个不同的启动屏幕

加载新视图在 iphone 中显示空白屏幕