2013-02-11 44 views
1

我想選擇的圖像從images.And的陣列選定的圖像應顯示在新視圖獲取..抓取選擇的圖像從一個數組顯示在另一個視圖

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

} 

燦有人幫我把選定的圖像提取到另一個視圖?在此先感謝..

+0

這段代碼有什麼問題? – 2013-02-11 13:15:40

+0

不能獲取我從數組中選擇的圖像。 – 2013-02-11 13:17:07

+0

應用程序包中的圖像是否包含圖像?或來自其他地方? – bobnoble 2013-02-11 13:18:01

回答

2

您正在設置字符串,而不是圖像。使用這段代碼

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

嘿Inder庫馬爾Rathore ..謝謝你..它現在工作很好.. – 2013-02-11 13:29:36

相關問題