I have a UIImageView object that I instantiate and turn into a property inside my iOS app. I initialize the attributes for this UIImageView object inside my viewDidLoad method, however, I need to change the image that is contained inside the UIImageView object later on in another method. When I do this, I for some reason am simply superimposing the new image over top of the old one. What I would like to do is simply resize the frame, and replace the old image with the new one.
Here is the relevant code:
Inside my viewDidLoad method:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 300, 200)];
imageView.image = [UIImage imageNamed:#"firstImage.png"];
[self.view addSubview:imageView];
and inside my method where I wish to make the changes:
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 200, 300)];
imageView.image = [UIImage imageNamed:#"secondImage.png"];
[self.view addSubview:imageView];
Please bear in mind that I don't want to just change the image, but also the frame size to accommodate the new image. Can anyone see what I am doing wrong?
You are creating another instance of imageview. You need to access the same object and change frame and image there.
Try this,
Create imageView as property in the class as,
In .h file,
#property(nonatomic, retain) UIImageView *imageView;
Then in viewDidLoad,
self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 250, 300, 200)];
self.imageView.image = [UIImage imageNamed:#"firstImage.png"];
[self.view addSubview:imageView];
The in second method, just change the frame and image as shown below,
self.imageView.frame = CGRectMake(10, 250, 200, 300);
self.imageView.image = [UIImage imageNamed:#"secondImage.png"];
As per you code, you were creating separate instances for imageViews and adding as subviews which will cause it to superimpose on another one. Instead you need to create a single instance as shown above and modify its frame and image properties whenever required.
Related
i create the the imageView by programatically and add that imageView to the Custom UIView.then i by click the button i want to add another imageView to the same custom UIView,here the old and new imageView also displaying,so i need to remove the first added imageView then i add the new imageView. help me to remove the imageView..
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];//add the imageView to UIView
When you add the first imageView set its tag like
imageView.tag = 232;
Later you can ask your customView for this imageView and remove it.
Call this function.
[[customView viewWithTag:232] removeFromSuperview];
Use this code.
[[self.customView viewWithTag:1000] removeFromSuperView]
CGRect rect=CGRectMake(10, 10, 100,100);
UIImageView *imageView=[[UIImageView alloc]init];
[imageView setTag:1000];
imageView.frame=rect;
imageView.backgroundColor=[UIColor redColor];
[self.customView addSubview:imageView];
I've to make something that looks like these:
I have a NSArray of walking and car objects.
Car class has a NSString property which contains these numbers.
How can I do this ? Thanks.
You could create UIImageViews and UILabels programatically and line them up in a cell/view whatever.
You could create a UIView as a container, with an UIImageView on the left and a UITextView on the right. This would be the template for the car object. Your walking object would just need a simple UIImageView. Then calculate the point you want the first UIImage to start at and add the next container at the end of the previous element. you could also create a template of your arrow, this way you just would need to connect the elements like you want. It should look like this
CGFloat fHeightImage = 20.0f;
UIView *view =
[[UIView alloc] initWithFrame:CGRectMake(0, 0,
[UIScreen mainScreen].bounds.size.width,
fHeightImage)];
UIImageView *imageViewCar =
[[UIImageView alloc] initWithFrame:CGRectMake(0, 0,
fHeightImage,
fHeightImage)];
[imageViewCar setImage:[UIImage imageNamed:#"walking"]];
[view addSubview:imageViewCar];
UIImageView *imageViewArrow =
[[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(imageViewCar.frame),
CGRectGetHeight(imageViewCar.frame),
fHeightImage,
fHeightImage)];
[imageViewArrow setImage:[UIImage imageNamed:#"arrow"]];
[view addSubview:imageViewArrow];
I am trying to display image in iphone simulator programmatically using objective c, I am using following code, but it is not displaying anything in simulator
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
You can use this code
imageview = [[UIImageView alloc] init];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
imageview.frame = CGRectMake(50, 50, 150, 150); // pass your frame here
[self.view addSubview:imageview];
Maybe you forgot this:
imageView.frame = self.view.bounds;
[self.view addSubview:imageview];
If you are programmatically creating the imageView then you must add it to the view heirarchy using below line
[self.view addSubview:imageview];
Add your image view into view like this
imageview = [[UIImageView alloc] initWithframe:CGRectMake(0, 0, 100, 200)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[self.view addSubview:imageview];
First you need to initialize your UIImageView and assign UIImage to it's image property. You already did it in your code. Then you can set the frame to image view.
imageview.frame = CGRectMake(50, 50, 150, 150);
But in most cases it will be more useful to pass sizeToFit message to your image view.
[imageview sizeToFit];
So it will change it's frame to fit image's size. But using this approach you need to assign the origin to your image view's frame later. So the best solution is assign the frame in initialization and then pass sizeToFit.
UIImageView *imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 20.0, 0.0, 0.0)];
UIImage *myimg = [UIImage imageNamed:#"A1.jpg"];
imageview.image=myimg;
[imageview sizeToFit];
And don't forget to add image view to view hierarchy.
[self.view addSubview:imageview];
Also you need to check if your image is correctly added to your project. You can check it by finding you image file in Project Navigator and checking it's Target Membership in File Inspector. There should be a tick beside you target's name.
How to set the image in Background image(means how to paste the image in Background image). I took one image as a background image in UIViewController. Right now i want to put the another image in background image.i know how to put the image as a Background image in UIViewController.But i have no idea how to put the image in background image.I tried the code.But the image will not displayed in background image.This is my code.please help me any body.Thanks in advance.
-(void)viewDidLoad
{
backgroundImage=[UIImage imageNamed:#"bg-small.PNG"];
audioViewBackgroundImage=[[UIImageView alloc]initWithImage:backgroundImage];
[self.view addSubview:audioViewBackgroundImage];
mImage=[UIImage imageNamed:#"muluguphoto.png"];
mBackgroundImage=[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage=[[UIImageView alloc]initWithImage:mImage];
[audioViewBackgroundImage addSubview:mBackgroundImage];
}
add it to your viewDidLoad..
UIImageView *newImage=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newImage setBackgroundColor:[UIColor redColor]];//here else you can add image
[self.view addSubview:newImage];
UITableView *newTable=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
[newTable setBackgroundColor:[UIColor clearColor]];
[self.view addSubview:newTable];
you have to set imageview above imageview in userinterface.like in photo.make proper heirARCHY!and set frame of imageview INDependentaly as you want.here is background image with logo image..
Add this line to viewDidLoad
self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:#"background.png"]];
Why do you initializing mBackgroundImage two times?
Let's try the below code and let me know the result.
UIImageView * audioViewBackgroundImage=[[UIImageView alloc] initWithFrame:self.view.frame];
audioViewBackgroundImage.image = [UIImage imageNamed:#"bg-small.PNG"];
[self.view addSubview:audioViewBackgroundImage];
UIImageView * mBackgroundImage =[[UIImageView alloc]initWithFrame:CGRectMake(20, 160, 150, 90)];
mBackgroundImage.image = [UIImage imageNamed:#"muluguphoto.png"];
[audioViewBackgroundImage addSubview:mBackgroundImage];
Thanks!
UIImageView * bgImage =[[UIImageView alloc]initWithFrame:self.view.frame];
bgImage.image = [UIImage imageNamed:#"myimg.png"];
[self.view addSubview:bgImage];
[self.view sendSubviewToBack:bgImage];
//write this line in viewdidload and make sure that you have import the image in your project by copying it .
when you write this line of code then this image will automatically replace the previous background image
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"stack.jpeg"]];
I'm developing a iOs app for iPad and I'm implementing a UIImageView inside a UIView. The problem is that if the position of the image view has a position out of the UIView that it is inside, you can see it. The code:
UIView *vistafunda = [[UIView alloc] initWithFrame:CGRectMake(512/2, 48, 525, 651)];
[self.view addSubview:vistafunda];
vistafunda.backgroundColor = [UIColor darkGrayColor];
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"hola.png"]];
img.center = CGPointMake(12, 200);
[vistafunda addSubview:img];
I would like that the image view was always under the view, so if the image is outside the view you could not see it.
Thanks!
Just make the containing view clip it's children
vistafunda.clipsToBounds = YES;