16

I just want to make an UIImageView programmatically that displays a 20by20 dot at point (50,50).(The image of the dot is called draw.png). For some reason nothing shows up on the screen.

Heres my code:

- (void)viewDidLoad
{
UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
dot.image=[UIImage imageNamed:@"draw.png"];
[self.view addSubview:dot];


[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib
}
3
  • Your code for adding imageView is right, show more code in this method or anything related. Commented Dec 29, 2013 at 2:21
  • Make sure that draw.png exists and if you are testing in device, check for case sensitivity. Commented Dec 29, 2013 at 2:54
  • I added the rest on top I want it to be made once the program begins Commented Dec 30, 2013 at 17:42

6 Answers 6

39

First, make sure draw.png exists in your project and that you are referencing it exactly (so if it's draw.PNG you should put @"draw.PNG"). Also, call [super viewDidLoad] before everything.

- (void)viewDidLoad
{
  [super viewDidLoad];
  // Do any additional setup after loading the view, typically from a nib

  UIImageView *dot =[[UIImageView alloc] initWithFrame:CGRectMake(50,50,20,20)];
  dot.image=[UIImage imageNamed:@"draw.png"];
  [self.view addSubview:dot];

}
Sign up to request clarification or add additional context in comments.

Comments

7

Try this :-

 UIImageView *imageview = [[UIImageView alloc]
 initWithFrame:CGRectMake(50, 50, 20, 20)];
 [imageview setImage:[UIImage imageNamed:@"AppleUSA1.jpg"]];
 [imageview setContentMode:UIViewContentModeScaleAspectFit];
 [self.view addSubview:imageview];

Comments

3

In Swift :

   let imageView = UIImageView(frame: CGRect(x: 50, y: 50, width: 20, height: 20))
     imageView.image = UIImage(named: "draw.png")
     self.view.addSubview(imageView)

Comments

2

This code is correct. Make sure draw.png exists in your project. You can do it by checking if [UIImage imageNamed:@"draw.png"] doesn't return nil.

Also, make sure you don't have another view on top of your image view.

1 Comment

I was missing .png from the image string reference. +1
2

make sure that image is in your project..

UIImageView *imgView=[[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 50, 50)];
[imgView setImage:[UIImage imageNamed:@"xyz.jpg"]];//if your images extension is .png than no need to write extension of an image..
[self.view addSubview:imgView];

Comments

2

This code is perfect match an UIImageView:

UIImageView *IMG_view= [[UIImageView alloc] initWithFrame:CGRectMake(20 ,50 ,40 ,40)];
[IMG_view setTag:100]; //[IMG_view setTag:indexPath.row];
IMG_view.layer.borderWidth= 0.5 ;
IMG_view.layer.borderColor= [[UIColor clearColor] CGColor];
IMG_view.layer.cornerRadius= 3;
IMG_view.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:IMG_view];
IMG_view.image=[UIImage imageNamed:@"Loading_50x50.png"];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.