xcode 在 UIImageView 中获取图像数组(在 UIPageViewController 中)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10299425/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting array of images in UIImageView (in UIPageViewController)
提问by user1048042
I've started from the standard PageView-based application template provided by Apple. Now instead of text I want the pages in the app to display images. An array is created as follows:
我从 Apple 提供的基于 PageView 的标准应用程序模板开始。现在,我希望应用程序中的页面显示图像而不是文本。创建一个数组如下:
_pageData = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:@"1.png"],
[UIImage imageNamed:@"2.png"],
[UIImage imageNamed:@"3.png"],
nil];
This array is handled the following way:
这个数组的处理方式如下:
DataViewController *dataViewController = [storyboard instantiateViewControllerWithIdentifier:@"DataViewController"];
dataViewController.dataObject = [self.pageData objectAtIndex:index];
return dataViewController;
Then, in DataViewController.h:
然后,在 DataViewController.h 中:
@interface DataViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) UIImage *dataObject;
And in DataViewController.m I'm trying to display the images in de imageView in the following way:
在 DataViewController.m 中,我尝试通过以下方式在 de imageView 中显示图像:
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
NSLog(@"dataObject %@", _dataObject);
}
However, with this code I'm not succeeding in getting the images onto the PageViewController. Any idea how I could get this working properly?
但是,使用此代码,我无法成功将图像放到 PageViewController 上。知道我如何才能正常工作吗?
A big thanks for you help!
非常感谢您的帮助!
采纳答案by Christoph
Here
这里
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
self.imageView = [[UIImageView alloc] initWithImage:_dataObject];
you are creating a new UIImageView that is dangling arround unused, invisible and without a parent. You need to set it's size somehow and put it to a parent view with a
您正在创建一个新的 UIImageView,它悬在未使用、不可见且没有父级的周围。您需要以某种方式设置它的大小并将其放入带有
[<parent> addSubview:self.imageView]
to show it.
显示它。
If your original UIImageView is read from a resource file ( I assume so because it is a IBOutlet ), just do a
如果您的原始 UIImageView 是从资源文件中读取的(我认为是因为它是 IBOutlet ),只需执行
self.imageView.image = _dataObject
and this should do the job instead.
这应该可以胜任。
回答by jcesarmobile
This worked for me
这对我有用
NSArray *anArray = [NSArray arrayWithObjects:
[UIImage imageNamed:@"transparente.png"],
[UIImage imageNamed:@"21001.jpg"],
[UIImage imageNamed:@"21002.jpg"],
[UIImage imageNamed:@"21003.jpg"],
[UIImage imageNamed:@"21004.jpg"],
[UIImage imageNamed:@"transparente2.png"],
nil];
_pageData = [anArray copy];
and in the dataViewController
并在 dataViewController
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
self.pageImage.image = self.dataObject;
}
The pageImage is an UIImageView with an IBOutlet.
pageImage 是一个带有 IBOutlet 的 UIImageView。