Image VIew problem - iphone

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];

Related

addSubview does not work

I will add a label to an Image with addSubview but this does not work.
here the code:
.h
UIImageView *bgimage;
IBOutet UILabel *loadingLabel;
.m
loadingLabel.text =#"......";
bgimage = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
bgimage.image = [UIImage imageNamed:#"Wait.png"];
[self.view addSubview:bgimage];
[bgimage addSubview:loadingLabel];
Screenshot:
"Tisch wird reserviert..." is the loadinglabel and is label shoud be in the UiImage
This is the "Layout" with the code from 2nd answer
Change your code as...
.h
IBOutet UIImageView *bgimage;
UILabel *loadingLabel;
.m
bgimage.image = [UIImage imageNamed:#"Wait.png"];
loadingLabel = [[UILabel alloc] init];
loadingLabel.frame = CGRectMake(0, 0, 100, 50);
loadingLabel.text = #"testing";
[bgimage addSubview:loadingLabel];
It will work.
You cant add a subview to a uiimageview, since drawRect: will never be called.
source:
"The "UIImageView class is optimized to draw its images to the display. UIImageView will not call the drawRect: method of a subclass. If your subclass needs custom drawing code, it is recommended you use UIView as the base class." from the apple docs:https://www.google.de/#bav=on.2,or.r_qf.&fp=90e2434f04e1ee9b&q=uiimageview+class+reference&safe=off
Solution:
As suggested, use a uiview to contain both the label and the imageview, and then bring the label to the top using -bringSubviewtoFront: of UIView. You can also use a UIViews backgroundimage to show your image and then have a label as a subclass in that view. Depends on the Situation you are in, i guess.
EDIT: YOU SHOULD READ THIS:
I misread the question, and it appears that you can add subviews to UIImageView, just like Zev pointed out in his comment. Right now im guessing that -bringSubviewToFront did the trick for you, and that the rest of my answer, while not really harmful, was unnecessary. Im sorry.

How do I move an existing image without Interface Builder

I created multiple images without the use of interface builder. I used this code:
for (Row* row in parser.rows) {
CGRect IphoneFrameImageRect;
IphoneFrameImageRect = CGRectMake(10, 10, 150.0f, 150.0f);
UIImageView *IphoneFrame = [[UIImageView alloc] initWithFrame:IphoneFrameImageRect];
NSString *IphoneFrameurl = [NSString stringWithFormat:#"http://some.url.com/iphone/IphoneFrame.png"];
[IphoneFrame setImage:[UIImage imageWithData: [NSData dataWithContentsOfURL: [NSURL URLWithString:IphoneFrameurl]]]];
[IphoneFrame setTag:(i)];
IphoneFrame.opaque = YES; // explicitly opaque for performance
[self.view addSubview:IphoneFrame];
[IphoneFrame release];
}
I need to move these images to a different location in landscape mode. I did tag each image and it looks I can select the images back using this tag. But how can I gave the image a new coordinate ? I have this code:
-(void)positionViews {
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
int i=4; //Picture with tag=4 for example
UIImage *tempImage=(UIImage *)[self.view viewWithTag:(i)];
// HOW TO MOVE ??
}
I am able to do this with a button using this code:
UIButton *tempButton=(UIButton *)[self.view viewWithTag:i];
[temp setFrame:CGRectMake(x, y, X10ButtonWidth, X10ButtonHeight)];
You are mixing up two different things / classes :
UIImage that represent image data, with its pixels, in memory
UIImageView that represent a view to display an image onscreen.
If it's more clear to you, an UIImage is to UIImageView what NSString is for an UILabel. The object representing the data/content is different from the view that can display it on screen.
So when you want to retrieve your imageview using the tag you set, you retrieve a view, especially an UIImageView for what matters, not an UIImage.
Change your cast to UIImageView and you will be able to call setFrame: on it the same way you do for your UIButton (as setFrame: is a method of the UIView class, and both UIImageView and UIButton are subclasses of UIView).
UIImageView *tempImageView = (UIImageView *)[self.view viewWithTag:(i)];
[tempImageView setFrame:...];
Or don't even do any cast, as viewWithTag: returns a UIView and that's all you need if you just want to change the frame: whether it's a basic UIView or specifically a UIImageView or whatever kind of view does not matter if you just want to change the frame property, as this is a property of the generic UIView class and can be applied to any UIView, whatever specific subclass of UIView it is.
UIView *tempImageView = [self.view viewWithTag:(i)];
[tempImageView setFrame:...];

Re-create UISwitch

My client doesn't like the gloss on the iOS 5 UISwitch, and wants me to create a non glossy version using some images. However I don't know how I would go a bouts this.
I have created a UIView Subclass and I don't know what to do next.
Any suggestions as to what I need to do next?
You can recreate UISwitch using CoreGraphics (without using images).
Have a look at this: DCRoundSwitch
DCRoundSwitch is designed to be a drop in replacement for UISwitch.
In any case you should create a UIControl subclass!
There are a number of ways to do what you want. One way is to use a UIImageView and just switch the image when the user taps on the control.
Something like:
-(void) loadView
{
[super loadView];
UIImageView *image1 = [UIImage imageNamed:#"image1-from-client"];
UIImageView *imageView = [[[UIImageView alloc] initWithImage:image1] autorelease];
imageView.userInteractionEnabled = YES;
[self.view addSubview:imageView];
// detect the tap
UITapGestureRecognizer *tapRecognizer = [[[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(tap:)] autorelease];
[imageView addGestureRecognizer:tapRecognizer];
}
// this gets called when the user taps
-(void) tap:(UIGestureRecognizer*)gesture
{
UIImageView *viewTapped = (UIImageView*)gesture.view;
if ( viewTapped.tag == 0 )
{
viewTapped.tag = 1;
viewTapped.image = [UIImage imageNamed:#"image2-from-client"];
}
else
{
viewTapped.tag = 0;
viewTapped.image = [UIImage imageNamed:#"image1-from-client"];
}
}
You can recreate a custom UISwitch by subclassing UIControl. Then you can use regular UIViews to create almost any effect you want. You can look take a look at SevenSwitch. A custom UISwitch replacement I've created. It is non-glossy and the colors can be customized to you're liking.
https://github.com/bvogelzang/SevenSwitch

remove image from tableViewCell created programmatically

I would like to move an imageView contained on a cell and it works but creating a new image at new position keeping old image (then two are shown). How could I remove old one?? used code:
UIImage *cellImage = [UIImage imageNamed:(#"%#", showIconName)];
UIImageView *imageViewToPutInCell = [[UIImageView alloc] initWithImage:cellImage];
imageViewToPutInCell.frame = CGRectMake(iconPos, 7, cellImage.size.width, cellImage.size.height);
[cell.contentView addSubview:imageViewToPutInCell];
every time that I reload tableView, it creates a new overlapped image.
[cell.contentView removeFromSuperview]; used before, removes it but then new image is not created.
There are a couple of ways to do this. Without subclassing, you could set the tag on your imageViewToPutInCell, then later on, use -[UIView viewWithTag:] to find and remove the view:
int imageViewTag = 1234;
[[cell.contentView viewWithTag: imageViewTag] removeFromSuperview];
imageViewToPutInCell.tag = imageViewTag;
...

UIImageView Class problem

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.