ios - 从 plist 中获取选定的图像以显示在另一个 View 中

标签 ios objective-c uiimage plist

从我的上一个问题开始,我修改了代码以从 plist 中获取图像。我的上一个问题在此链接中( Fetching Selected Image From An Array to be displayed in another view )

我的代码是:

- (void)viewDidLoad
{

 [super viewDidLoad];

    NSDictionary *dictionary = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ImageList" ofType:@"plist"] ];
self.arrayImages = [dictionary objectForKey:@"Images"];

selectedTag = 0;
images = [[NSMutableArray alloc]init];
lists = [[NSMutableArray alloc]init];
imageScroll.delegate = self;
imageScroll.scrollEnabled = YES;
int scrollWidth = 768;
imageScroll.contentSize = CGSizeMake(scrollWidth,605);
int xOffset = 0;
int total = self.arrayImages.count;

for(index=0; index < total; index++)
{
    dict = [self.arrayImages objectAtIndex:index];

    UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
    UILabel *lab = [[UILabel alloc] init];
    img.tag = index;
    selectedTag = img.tag;
    img.bounds = CGRectMake(10, 10, 50, 50);
    img.frame = CGRectMake(50+xOffset, 120, 270, 350);
    lab.bounds = CGRectMake(20, 20, 150, 150);
    lab.frame = CGRectMake(160+xOffset, 500, 90, 50);
    lab.font = [UIFont systemFontOfSize:24];
    [img setImage:[UIImage imageNamed:[dict objectForKey:@"imagesName"]] forState:UIControlStateNormal];
    lab.text = [NSString stringWithFormat:[dict objectForKey:@"listName"]];
    [images insertObject:img atIndex:index];
    [lists insertObject:lab atIndex:index];
    imageScroll.contentSize = CGSizeMake(scrollWidth+xOffset,510);
    [imageScroll addSubview:[images objectAtIndex:index]];
    [imageScroll addSubview:[lists objectAtIndex:index]];
    xOffset += 370;

    [img addTarget:self action:@selector(newView:) forControlEvents:UIControlEventTouchUpInside];
    selectedTag = img.tag;
}    
}

-(void)newView:(id)sender{

NSLog(@"%@",sender);
int newIndex;
newIndex = [sender tag];
NSLog(@"Selected Tag :%d",newIndex);
CGRect rec = CGRectMake(0, 0, 250, 250);
[self setView:[[[UIView alloc] initWithFrame:rec] autorelease]];
[[self view] setBackgroundColor:[UIColor whiteColor]];
UIImage *img = [UIImage imageNamed:[[dict objectForKey:@"imagesName"]objectAtIndex:newIndex]];

UIImageView *im = [[UIImageView alloc] initWithImage:img];
UILabel *lab1 = [[UILabel alloc] init];
lab1.text = [NSString stringWithFormat:[[dict objectForKey:@"listName"] objectAtIndex:newIndex]];


lab1.bounds = CGRectMake(20, 20, 150, 150);
lab1.frame = CGRectMake(360, 600, 90, 50);
lab1.font = [UIFont systemFontOfSize:24];
im.bounds = CGRectMake(10, 10, 50, 50);
im.frame = CGRectMake(200, 120, 370, 450);
[[self view] addSubview:im];
[[self view] addSubview:lab1];
}

我的 Plist 输出:

   Images =     (
            {
        Contents = "Sea While Tsunami";
        imagesName = "sea1.jpg";
        listName = Sea1;
    },
            {
        Contents = "Sea While Tsunami";
        imagesName = "sea2.jpg";
        listName = Sea2;
    },
            {
        Contents = "Sea While Tsunami";
        imagesName = "sea3.jpg";
        listName = Sea3;
    },
            {
        Contents = "Sea While Tsunami";
        imagesName = "sea4.jpg";
        listName = Sea4;
    },
            {
        contents = "Sea While Tsunami";
        imagesName = "sea5.png";
        listName = Sea5;
    },


 ),

我收到的错误为:

<UIButton: 0x714f3a0; frame = (420 120; 270 350); opaque = NO; tag = 1; layer = <CALayer: 0x4f460>>
2013-02-12 12:58:46.973 image[2546:11303] Selected Tag :1
2013-02-12 12:58:46.975 image[2546:11303] -[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x714bd60
2013-02-12 12:58:46.976 image[2546:11303] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString objectAtIndex:]: unrecognized selector sent to instance 0x714bd'
***First throw call stack:
(0x8f012 0xcce7e 0x1a4bd 0x7ebbc 0x7e94e 0x3584 0xe0705 0xe97e3 0xe9668 0x1465c 0x26a2 0x25d5)
libc++abi.dylib: terminate called throwing an exception

plist 工作正常..它在第一个 View 中显示良好..但是在将所选图像提取到另一个 View 时我收到此错误..我在 newView: 中初始化提取的图像时犯了错误..有人可以帮我解决这个问题吗?提前致谢..

最佳答案

YurSecondViewController.h中声明它

UIImage *img;
@property(nonatomic,retain)UIImage *img;

确保您创建了属性合成img

在拥有该图像FirstViewController中:-

YurSecondViewController *View=[[[YurSecondViewController alloc]init];
View.img=[UIImage imageNamed:@"Default.png"];
[self.navigationController pushViewController:View animated:YES];

在 YurSecondViewController.m 中,您必须显示图像:-

UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(50, 50, 100, 100)];
[imgView setImage:self.img];
[self.view addSubview:imgView];

关于ios - 从 plist 中获取选定的图像以显示在另一个 View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14827877/

相关文章:

ios - Iphone 模拟器屏幕键盘问题

objective-c - 没有可见的@interface

objective-c - 如何使用 objective-c 获取ipad目录中的所有文件

objective-c - 如何在 NSOutlineView 中创建右键菜单?

ios - 带有 alpha channel 的 UIImage cv::Mat 转换

ios - URL 中的 UIImage 需要太多时间才能显示

iphone - 为每个 Spotify 用户加载仅由一个用户创建的播放列表

ios - CLLocation 没有给出实际速度

xcode - 在 xcode 3.2.3 中为 iPhone 本地化图像时出现问题

objective-c - 通过代码将标题/图标等设置为在 IB 中创建的 TabBarItem?