iPhone 上动态创建的 UIImageViews 未显示
我已经达到了headdesk的目的,尝试在iOS 4.2中动态创建UIImageView。
这是给我带来麻烦的代码:
设置
UIImageView *iview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 95, 149)];
iview.tag = 200; // see note 1
iview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:iview];
[iview release];
1)我尝试过释放和不释放 iview 的
。 2)回报
NSString *img = ...[see note 2];
NSLog(@"Filename: %@", img);
UIImageView *pIV = (UIImageView *)[self.view viewWithTag:200];
if (pIV == nil) {
abort();
}
UIImage *pI = [UIImage imageWithContentsOfFile:img];
if (pI == nil) {
abort();
}
[pIV setImage:pI];
这被扩展以显示我正在查看的内容,因为简洁的版本不适合我。
注 1:上述大多数硬编码值(200、帧边界等)要么是#defines,要么是在 iview 分配代码所在的 forloop 中计算的。为了简洁而省略,但如果您认为相关,请告诉我。
注 2:这只是一条在查看 NSBundle 等后返回文件路径的消息。NSLog 输出是
2011-01-30 22:09:54.767 ribbit[13529:207] 文件名:/Users/rip/Library/Application Support/iPhone Simulator/4.2/Applications/C421AA9F-45BB-41AB-8CB0-1540FABB56D3/ribbit.应用程序/cardfront_a7.png
.png 确实存在,并且两个哨兵都不为零(因此 abort() 都不会发生)。
我确信这只是一些基本的东西。
我尝试过的事情:
1)使用IB生成UIImageView。当它正确连接时,我可以看到框架,并且显示 IB 中默认给出的图像。然而,我仍然无法更改图像,这就是为什么我开始动态创建它们......认为这会更简单。呃。
2)尝试对各种iview使用实例变量,但这很笨拙(正在生成十个这样的UIImageView)并且它们也不起作用。
因为所有三种尝试都因相同的行为而失败(运行良好,只是看不到图像),所以我假设这只是一些愚蠢的小事情。
我欢迎您提出任何建议。
I've achieved the point of headdesk, trying to dynamically create UIImageView in iOS 4.2.
This is the code that is causing me trouble:
1) The setup
UIImageView *iview = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 95, 149)];
iview.tag = 200; // see note 1
iview.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin;
[self.view addSubview:iview];
[iview release];
I've tried with both releasing and not releasing iview.
2) The payoff
NSString *img = ...[see note 2];
NSLog(@"Filename: %@", img);
UIImageView *pIV = (UIImageView *)[self.view viewWithTag:200];
if (pIV == nil) {
abort();
}
UIImage *pI = [UIImage imageWithContentsOfFile:img];
if (pI == nil) {
abort();
}
[pIV setImage:pI];
This is expanded to show what I'm looking at, since the terse version wasn't working for me.
Note 1: Most of the above hardcoded values (200, the frame bounds, etc) are either #defines or computed in the forloop that the iview alloc code is within. Left out for brevity, but let me know if you think it's relevent.
Note 2: This is just a message that returns the filepath after looking in NSBundle etc. The NSLog output is
2011-01-30 22:09:54.767 ribbit[13529:207] Filename: /Users/rip/Library/Application Support/iPhone Simulator/4.2/Applications/C421AA9F-45BB-41AB-8CB0-1540FABB56D3/ribbit.app/cardfront_a7.png
The .png does exist, and both the sentinels aren't nil (so neither abort() happens).
I'm convinced that it's just something basic.
Things I've tried:
1) Used IB to generate the UIImageView. When it was wired up correctly, I could see the frame, and the image that was given as the default in IB showed. I still could not, however, alter the image, which is why I moved to dynamically creating them...thought it would be simpler. Eh.
2) Tried using instance variables for the various iviews, but that was unwieldy (there are ten of these UIImageView being generated) and they didn't work either.
Because all three attempts failed with the same behavior (runs fine, just can't see the images), I'm assuming that it is just some silly little thing.
I welcome any suggestions you might have.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题似乎出在 setImage(没有)和 opaque=YES 上。
我预先创建了 UIImageViews,然后根据需要用图像延迟加载它们,这就是为什么 setImage 不在上面的代码示例中的原因。现在我这样做了,而且似乎正在发挥作用:
一旦我添加了这些,卡片就会出现。问题可能不是/未/被加载或显示,而是可见性之一。
无论如何,搞砸了。
The issue appeared to be with the setImage (there wasn't one) and opaque=YES.
I was pre-creating the UIImageViews, and then loading them with an image lazily, as needed, which is why the setImage wasn't in the above code examples. Now I do, and it seems to be working:
Once I'd added those, the cards appeared. It may be that the problem was not /not/ being loaded or displayed, rather it was one of visibility.
Anyway, fuggedaboutit.