I have taken an Imageview class and iam adding image into that with code:
TileImageView *tileImageView = [[Tile alloc] initWithImage:tileImage];
and adding into view like this:[self.view insertSubview:tileImageView atIndex:0];
So how to remove the Imageview class and alloc new image to that view
There's a tag property in the UIView class which you can use to retrieve the view later.
titleImageView.tag = 1;
[self.view addSubview:titleImageView];
// later:
titleImageView = [self.view viewWithTag:1];
titleImage.view.image = otherImage;
Or you can save the reference to the image view in an instance variable.
Related
What i am trying to do is i read some content from a web service, each content has content type (text,image,button...).
I have to display dynamically the content in the UIView. So what i am trying to do is to create an IBoutlet UIView and check if the content type is a text i convert this uiview to UILable and display the text, if the content type is an image i convert the UIView to a UIImageView etc...
I need to work on the same UIView from the xib file, so how can i change the properties of the UIView to a UILable or a UIImageView.
Thank you
You cannot "change" an UIView to be something different.
I suggest you dynamically add subviews to your view:
UIView *newView = nil;
switch (content type) {
case 0: // text
newView = [[UILabel alloc] init];
((UILabel*)newView).text = #"someText";
[newView sizeToFit];
case 1: // button
{
UIButton *btn = [UIButton buttonWithType:...];
[btn setTititle:#"Ttile" forControlState:UIControlStateNormal]; // assign all needed properties
...
newView = btn;
}
case 2:
....
}
newView.frame = ... // calculate Frame using previously created content (e.g. remember y offset and add bounds.size.height after every iteration)
[self.view addSubView:newView];
....
Before i click the image to pull the camera, i want to set up a default image on the imageview that shows a http://www.inc.com/images/avatar/default1.gif to inform that in that imageview goes a person image.
What is the best way to do this procedure?
Best regards.
If you are using a UIImageView the first thing is to create a UIImage object using this code
UIImage *defaultImage = [UIImage imageNamed:#"default.png"];
And then pass it to the UIImageView object if it is a property of your view controller you can use this(imageView is the variable name of UIImageView)
self.imageView.image = defaultImage;
If you are instantiating a new UIImageView in your view controller you can use this
UIImageView *imageView = [[UIImageView alloc] initWithImage:defaultImage];
UIImageView *theImageView;
//assuming that's the declaration in your header
//make sure default1.gif is inside your application bundle
//copied into the resources folder of your xcode project
//this code goes into your -viewDidLoad method
theImageView.image = [UIImage imageNamed:#"default1.gif"];
I have an app where it reads an XML document containing 2 fields (image url and description). If the document has a field that has a valid image URL, I want the view to show the image. Else I just want it to show the description.
The problem I am having is :
how to show the UIImageView dynamically
how to move the UITextView downwards since now I added a UIImageView
Currently I just have a View with a UITextView on it.
Any ideas?
1) -[UIView setHidden:] if the UIImageView is already in place. Otherwise, [[UIImageView alloc] initWithFrame:], then add the image view as a subview to the view.
2) -[UIView setFrame:] (is one option)
The following is what you could do.
Prepare the the view with the UIImageView and the UITextView in the viewDidLoad like this:
-(void) viewDidLoad)
{
[super viewDidLoad];
myImageView = [[UIImageView alloc] init];
myImageView.frame = CGRectMake(x,y,0,0); //you can set it at the right position and even set either the width OR the height depending on where you want the textView in my example I'm gonna assume its beneath the imageView
//other imageView settings
[myView addSubView:myImageView];
myTextView = [[myTextView alloc] init];
myTextView.frame = CGRectMake(x,y,width,heigh+CGRectGetMaxY(myImageView.frame)); //here you prepare the textView for the imageView and the space it takes. But doesn't get hindered by it if it's not there.
//other textView settings.
[myView addSubView:myTextView];
//You would want this loading function about here OR after this loading has been occured.
[self loadImageFromURL];
}
-(void)loadImageFromURL
{
//get your image here.. or not
if(imageHasLoaded) //some condition that gets set when your image has successfully loaded. (This should be done in a delegate (didLoadImageFromURL) if you have such thing prepared.
{
//myImageView.image = theLoadedImage;
myImageView.frame = CGRectMake(x,y,theLoadedImageWidth,theLoadedImageHeight);
//after you set the frame of the imageView you need to refresh the view
[myView setNeedsDisplay];
}
}
And this is how you should dynamically add an imageView with image and frame to some view.
How can i remove an ImageView added on a dynamically created UIView, so that i can add another ImageView on my UIView.
You either add a tag for that UIImageView and find it based on tag or loop throughout the subviews and look for an object of class UIImageView containing the image you need to change.
Easiest way is probably with tags. So...
UIImageView *removeMe = [[UIImageView alloc] init];
removeMe.image = [UIImage imageNamed:#"theImage.png"];
removeMe.tag = 1;
[theView addSubview:removeMe];
[removeMe release]; //theView now retains it!
...then later:
UIImageView *removalTarget = (UIImageView *)[theView viewWithTag:1];
[removalTarget removeFromSuperview];
I am using a custom subclass of UIImageView, but I can't figure out why it's not being displayed.
The relevant code from my UIImageView subclass:
-(id) initWithImage:(UIImage*)image{
if(self = [super initWithImage:image]){
}
return self;
}
And from the view controller for the view that will be displaying my subclass:
UIImage *zoomRatateSliderHorizontal =
[[UIImage imageNamed:#"ZoomRotate Slider Horizontal.png"]
stretchableImageWithLeftCapWidth:(75.0)/2-1.0 topCapHeight:0];
scaleHorizontalControl = [[ViewTransformationController alloc]
initWithImage:zoomRatateSliderHorizontal];
scaleHorizontalControl.onScreenFrame = CGRectMake(0.0, 5.0,
self.view.frame.size.width,
scaleHorizontalControl.image.size.height);
scaleHorizontalControl.frame = scaleHorizontalControl.onScreenFrame;
scaleHorizontalControl.offScreenFrame = CGRectZero;
[self.view addSubview:scaleHorizontalControl];
Before I was subclassing, I had no trouble with the following in the view controller:
UIImage *zoomRatateSliderVertical =
[[UIImage imageNamed:#"ZoomRotate Slider Vertical.png"]
stretchableImageWithLeftCapWidth:0 topCapHeight:(75.0)/2-1.0];
scaleOrRatateViewVertical = [[UIImageView alloc]
initWithImage:zoomRatateSliderVertical];
scaleOrRatateViewVertical.frame = CGRectMake(-zoomRatateSliderVertical.size.width,
zoomRatateSliderHorizontal.size.height+5.0+5.0,
zoomRatateSliderVertical.size.width,
465.0 - zoomRatateSliderHorizontal.size.height-10.0-5.0);
[self.view addSubview:scaleOrRatateViewVertical];
Using break points I checked the frame and image being passe to my class, and they both appear to be valid and desired.
Any advice on what I'm doing wrong I'd greatly appreciate.
The Apple Docs read:
The UIImageView class is optimized to draw its images to the display. UIImageView will not call drawRect: a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class.
So I'd subclass UIView and work from there.