全部 任何人都可以参考我,iPhone图像库源代码? 任何链接,示例代码都会有很大的帮助。
我试图以缩略图显示大约70到100张图像,并且在选择任何图像时,它应该提供该图像的完整视图,我试图完成,无论iphone的图片库中是什么, 我想,必须有任何示例代码。 建议总是受到赞赏
问候
答案 0 :(得分:8)
在didload方法中调用两个方法来创建滚动视图和缩略图按钮。以相同的顺序保存缩略图图像和壁纸图像数组。
-(void)createScrollView
{
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(320 * (([imagesArray count]- 1) / 25 + 1), 440);
scrollView.backgroundColor=[UIColor blackColor];
[self.view addSubview:scrollView];
}
-(void)createButton{
for (int i = 0; i < [imagesArray count]; i++)
{
thumbNailButton = [UIButton buttonWithType:UIButtonTypeCustom];
thumbNailButton.frame = CGRectMake(6 + 62 * (i % 5) + 320 * (i / 25), 5+65 * ((i / 5) % 5), 56,56);
img = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 56, 56)];
[img setImage:[UIImage imageNamed:[imagesArray objectAtIndex:i]]];
thumbNailButton.tag=i;
[thumbNailButton addTarget:self action:@selector(imageClicked:) forControlEvents:UIControlEventTouchUpInside];
[thumbNailButton addSubview:img];
[scrollView addSubview:thumbNailButton];
}
}
-(void)imageClicked:(id)sender{
UIButton* button = (UIButton*)sender;
AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
[appDelegate setimageClickedValue:button.tag];
LargeWallPaperviewController *largeWallPaperViewController=[[LargeWallPaperviewController alloc]initWithNibName:@"LargeWallPaperviewController" bundle:nil];
[self.navigationController pushViewController:largeWallPaperViewController animated:YES];
[largeWallPaperViewController release];
}
在didload方法的largewallpaperviewcontroller类中
[imagesArray addObjectsFromArray:[NSArray arrayWithObjects:@"wallf1.jpg",@"wallf2.jpg",@"wallf3.jpg",@"wallf4.jpg",@"wallf5.jpg",@"wallf6.jpg",@"wallf7.jpg",nil]];
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 440)];
scrollView.pagingEnabled = YES;
scrollView.showsHorizontalScrollIndicator = NO;
scrollView.showsVerticalScrollIndicator = NO;
scrollView.scrollsToTop = NO;
scrollView.delegate = self;
scrollView.contentSize = CGSizeMake(320 * ([imagesArray count] ), 440);
scrollView.backgroundColor = [UIColor blackColor];
[self.view addSubview:scrollView];
for (int i = 0; i < [imagesArray count]; i++)
{
wallPaperButton=[UIButton buttonWithType:UIButtonTypeCustom];
wallPaperButton.tag=i;
wallPaperButton.frame=CGRectMake((320*i),0, 320, 325);
UIImageView *img =[ [UIImageView alloc]initWithFrame:CGRectMake(0,0, 320, 325)];
img.image=[UIImage imageNamed:[imagesArray objectAtIndex:i]];
img.contentMode=UIViewContentModeScaleAspectFit;
[wallPaperButton addSubview:img];
[img release];
[wallPaperButton addTarget:self action:@selector(imageSelected:) forControlEvents:UIControlEventTouchUpInside]; [scrollView addSubview:wallPaperButton];
}
appDelegate=(AppDelegate *)[[UIApplication sharedApplication]delegate];
int imageValue=[appDelegate getimageClickedValue];
[scrollView scrollRectToVisible:CGRectMake(320*imageValue, 0, 320 , 440) animated:NO];
我在largewallpaer视图中使用了一个按钮。如果你想要删除它并直接将其添加到图像视图。这段代码对我有用,可以根据你的要求进行更改。它很容易理解。
一切顺利。
答案 1 :(得分:1)
向这段代码致敬。这非常有效。虽然自从我使用故事板以来我修改了“appdelegate部分”。
这就是我做的事情
-(void)imageClicked:(id)sender{
UIButton* button = (UIButton*)sender;
imageNumber = button.tag;
[self performSegueWithIdentifier:@"loadImage" sender:sender];
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"loadImage"])
{
[segue.destinationViewController setimageValue:imageNumber];
}
}
在名为View Controller.m
的目标'ImageViewController'
文件中,我实现了以下内容
-(void)setimageValue:(int)number;
{
self.imageValue = number;
}
在viewDidLoad
中,我只使用
[self.scrollView scrollRectToVisible:CGRectMake(320*self.imageValue, 0, 320 , 440) animated:NO];