Objective C:以编程方式创建 UIImageView
我正在尝试制作一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试
看到对其他答案的回复后
编辑:您是否在其他地方声明和初始化brickAnim?如果没有,则需要在开头添加:
并在结尾添加:
try
edit after seeing response to other answer:
are you declaring and initializing brickAnim elsewhere? if not, you need to add at the beginning:
and at the end:
您需要首先创建一个 UIImage,因为 setImage 需要一个 UIImage 对象。像这样的东西可能会起作用(请注意,这是一个类方法):
查看 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):
Look at the UIImage reference, i'm not sure this will work (since imageNamed may want a different path-format).