Objective C:以编程方式创建 UIImageView

发布于 2024-10-12 00:07:14 字数 366 浏览 3 评论 0原文

我正在尝试制作一个 UIImageView,在单击按钮时将 .png 加载到按钮的位置。

brickAnim = UIImageView.alloc;           ///////freezes during runtime
[brickAnim initWithFrame:currentBrick.frame];
[brickAnim setImage:[NSString stringWithFormat:@"brick-1.png"]];
[self.view addSubview:brickAnim];

当前块是正在单击的按钮的名称。我缩小了范围,发现第一行导致应用程序冻结并退出。我不明白我做错了什么。

I'm trying to make a UIImageView with a .png load up in the location of a button when it is clicked.

brickAnim = UIImageView.alloc;           ///////freezes during runtime
[brickAnim initWithFrame:currentBrick.frame];
[brickAnim setImage:[NSString stringWithFormat:@"brick-1.png"]];
[self.view addSubview:brickAnim];

current brick is the name of the button that's being clicked. I've narrowed it down and figured out that the first line causes the app to freeze and exit. I can't figure out what I'm doing wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

淡忘如思 2024-10-19 00:07:14

尝试

brickAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick-1.png"]];
brickAnim.frame = currentBrick.frame;
[self.view addSubview:brickAnim];

看到对其他答案的回复后

编辑:您是否在其他地方声明和初始化brickAnim?如果没有,则需要在开头添加:

UIImageView *brickAnim = [[UIImageView alloc] ....;

并在结尾添加:

[brickAnim release];

try

brickAnim = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"brick-1.png"]];
brickAnim.frame = currentBrick.frame;
[self.view addSubview:brickAnim];

edit after seeing response to other answer:

are you declaring and initializing brickAnim elsewhere? if not, you need to add at the beginning:

UIImageView *brickAnim = [[UIImageView alloc] ....;

and at the end:

[brickAnim release];
诺曦 2024-10-19 00:07:14

您需要首先创建一个 UIImage,因为 setImage 需要一个 UIImage 对象。像这样的东西可能会起作用(请注意,这是一个类方法):

[brickAnim setImage:[UIImage imageNamed:@"brick-1.png"]];

查看 UIImage 参考,我不确定这是否会起作用(因为 imageNamed 可能需要不同的路径格式)。

You need to create a UIImage first, since setImage wants an UIImage object. Something like this may work (note that this is a class method):

[brickAnim setImage:[UIImage imageNamed:@"brick-1.png"]];

Look at the UIImage reference, i'm not sure this will work (since imageNamed may want a different path-format).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文