user842059
user842059

Reputation: 73

CGRectMake for UIImageView?

I am trying to use CGRectMake to assign a place and size to a uiimageview, here is the code;

     -(IBAction)done11 {

  [self performSelector:@selector(multibuttonvoid) withObject:nil afterDelay:2.0];



      }

  -(void)multibuttonvoid {

 UIImageView *multibtn = [UIImageView alloc];
multibtn.image = [UIImage imageNamed:@"multibuttonpic"];

multibtn = CGRectMake(159, 325, 438, 318);
  [self.view addSubview:multibtn];


      }

So, as you can see if i press a button it should add a uiimageview with a picture. But for some reason, i get this error on the CGRectMake line: Assigning uiimageview to incompatible type CGRect, I thought a CGRect was a uiimage

Upvotes: 1

Views: 5546

Answers (3)

Anomie
Anomie

Reputation: 94794

A CGRect, a UIImage, and a UIImageView are completely different things.

CGRect is a simple stucture defining a rectangle in some arbitrary coordinate space, using a point (origin) and size (size).

A UIImage is an image and associated metadata.

A UIImageView is a UIView that is intended for displaying a UIImage.

It looks like what you really want is to set the frame property of your UIImageView, to instruct it as to where on the screen it should display:

multibtn.frame = CGRectMake(159, 325, 438, 318);

Also, BTW, don't forget to initialize your UIImageView, by calling either initWithImage:, initWithFrame:, or the like on it. This is usually done at the same time as the allocation:

UIImageView *multibtn = [[UIImageView alloc] initWithImage:...];

Upvotes: 5

user756245
user756245

Reputation:

You didn't initialize your UIImageView. Try calling -(id)initWithImage:(UIImage *)image; givig nil at first, and then setting both image and frame properties.

UIImageView *multibtn = [[UIImageView alloc] initWithImage:nil];
multibtn.image = [UIImage imageNamed:@"multibuttonpic"];
multibtn.frame = CGRectMake(159, 325, 438, 318);
[self.view addSubview:multibtn];

or

UIImageView *multibtn = [[UIImageView alloc]
                         initWithImage:[UIImage imageNamed:@"multibuttonpic"]];
multibtn.frame = CGRectMake(159, 325, 438, 318);
[self.view addSubview:multibtn];

Upvotes: 2

omz
omz

Reputation: 53551

A CGRect, as the name implies, represents a rectangle, not an image, so you have to assign it to a property of your image view that is a rectangle, the frame in this case.

Upvotes: 1

Related Questions