gpt4 book ai didi

iOS 如何将图像索引从 collectionview 传递到 scrollview

转载 作者:行者123 更新时间:2023-11-29 13:17:12 25 4
gpt4 key购买 nike

我正在创建一个程序,如果我在 collectionview 中单击缩略图,则应在另一个 View 收集器中的 ScrollView 中打开较大的图像。为此,我正在使用 segue。但是我在那里做错了,我该如何解决这个问题,

我的 segue 代码是,

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"])
{
NSIndexPath *selectedIndexPath = [[self.gallerycollection indexPathsForSelectedItems] objectAtIndex:0];

// load the image, to prevent it from being cached we use 'initWithContentsOfFile'
NSString *imageNameToLoad = [NSString stringWithFormat:@"interior_%d", selectedIndexPath.row];
NSString *pathToImage = [[NSBundle mainBundle] pathForResource:imageNameToLoad ofType:@"jpg"];
UIImage *image = [[UIImage alloc] initWithContentsOfFile:pathToImage];

GalleryImageScrollViewController *gallerydetailViewController = [segue destinationViewController];
gallerydetailViewController.FullScreenImageScroller=image ;
}
}

detailViewController.m

- (void)viewDidLoad
{
[super viewDidLoad];


NSMutableArray *mutablearray = [NSMutableArray array];
data=[MyDatabase new];
slideImages=[data OpenMyDatabase:@"SELECT pic_name_big FROM interior":@"pic_name_big"];
[mutablearray addObjectsFromArray:slideImages];
temparr = [[NSMutableArray alloc]init];
temparr=[NSMutableArray arrayWithArray:mutablearray];
[self putImageViewsInScrollView:[temparr count]];
self.FullScreenImageScroller.delegate=self;

}




-(void) putImageViewsInScrollView:(int)numberOfImageViews
{
for(int i=0 ;i<numberOfImageViews; i++)
{
fullScreenImageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:[temparr objectAtIndex:i]]];
fullScreenImageView.frame = CGRectMake((WIDTH_OF_IMAGE * i) , 0, WIDTH_OF_IMAGE, HEIGHT_OF_IMAGE);
[self.FullScreenImageScroller addSubview:fullScreenImageView];
}
[self.FullScreenImageScroller setContentSize:CGSizeMake(WIDTH_OF_SCROLL_PAGE * ([temparr count]), HEIGHT_OF_IMAGE)];
[self.FullScreenImageScroller setContentOffset:CGPointMake(0, 0)];
[self.FullScreenImageScroller scrollRectToVisible:CGRectMake(WIDTH_OF_IMAGE,0,WIDTH_OF_IMAGE,HEIGHT_OF_IMAGE) animated:NO];


}

那么我应该在 ScrollView Controller (GalleryImageScrollViewController)中传递什么来打开特定缩略图的图像?

最佳答案

像这样将图像名称传递给详细 View Controller

-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSArray *indexpaths = [self.collectionView indexPathsForSelectedItems];
DetailViewController *dest = [segue destinationViewController];
dest.imageName = [recipeImages objectAtIndex:[[indexpaths objectAtIndex:0] row]];
// imageName is a property of detail view controller
}

然后在detailViewController的viewDidLoad

- (void)viewDidLoad
{
[super viewDidLoad];

self.imageView.image = [UIImage imageNamed:self.imageName];
}

我没有使用 ScrollView ,只有一个 imageView 作为 subview 添加到 View 中

enter image description here

关于iOS 如何将图像索引从 collectionview 传递到 scrollview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15425000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com