Goal C: Programmatically Create a UIImageView

I am trying to make a UIImageView with loading .png at the location of the button click on click.

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 the button is pressed on. I narrowed it down and realized that the first line causes the application to freeze and exit. I cannot understand what I am doing wrong.

+4
source share
2 answers

to try

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

edit after viewing the answer to another answer:

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

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

and in the end:

 [brickAnim release]; 
+13
source

First you need to create a UIImage since setImage wants a UIImage object. Something like this might work (note that this is a class method):

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

Look at the UIImage link, I'm not sure if this will work (since imageNamed may require a different path format).

+8
source

Source: https://habr.com/ru/post/1335276/


All Articles