从数组中获取所选图像以在另一视图中显示 [英] Fetching Selected Image From An Array to be displayed in another view

查看:85
本文介绍了从数组中获取所选图像以在另一视图中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从图像数组中提取所选图像。所选图像应以新视图显示..

I'm trying to fetch the selected image from the array of images.And the selected image should be displayed in new view..

- (void)viewDidLoad{
[super viewDidLoad];

selectedTag = 0;

imagesName = [[NSArray alloc]initWithObjects:@"nature1.jpg",@"nature2.jpg",@"nature3.jpg",@"nature4.jpg",@"nature5.png",@"nature6.jpg",nil];
images = [[NSMutableArray alloc]init];

imageScroll.delegate = self;
imageScroll.scrollEnabled = YES;
int scrollWidth = 768;
imageScroll.contentSize = CGSizeMake(scrollWidth,605);

int xOffset = 0;

for(index=0; index < [imagesName count]; index++)
{

    UIButton *img = [UIButton buttonWithType:UIButtonTypeCustom];
    img.tag = index;
    selectedTag = img.tag;

    img.bounds = CGRectMake(10, 10, 50, 50);
    img.frame = CGRectMake(50+xOffset, 120, 270, 350);

    NSLog(@"image: %@",[imagesName objectAtIndex:index]);
    [img setImage:[UIImage imageNamed:[imagesName objectAtIndex:index]] forState:UIControlStateNormal];

    [images insertObject:img atIndex:index];
    imageScroll.contentSize = CGSizeMake(scrollWidth+xOffset,510);
    [imageScroll addSubview:[images objectAtIndex:index]];
    xOffset += 370;


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

NSLog(@"%@",sender);
int f;
f = [sender tag];


CGRect rec = CGRectMake(0, 0, 250, 250);
[self setView:[[[UIView alloc] initWithFrame:rec] autorelease]];
[[self view] setBackgroundColor:[UIColor grayColor]];
UIImageView *im = [[UIImageView alloc] initWithImage:[imagesName objectAtIndex:f]];


im.bounds = CGRectMake(10, 10, 50, 50);
im.frame = CGRectMake(250, 120, 270, 350);


[[self view] addSubview:im];

}

任何人都可以帮我将所选图片提取到另一个视图??在此先感谢..

Can anybody help me to fetch the selected image to another view?? Thanks In Advance..

推荐答案

您正在设置字符串而不是图像。使用这段代码

You are setting string instead of image. Use this piece of code

UIImage *img = [UIImage imageNamed:[imagesName objectAtIndex:f]];
UIImageView *im = [[UIImageView alloc] initWithImage:img];

这篇关于从数组中获取所选图像以在另一视图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆