Updating UIImage within arrayed UIImageview - iphone

I can now display a UIImageView that is within an array but using the following:
[[self.guess objectAtIndex:1] setFrame:CGRectMake(20, 20, 100, 100)];
[self.view addSubview:[self.guess objectAtIndex:1]];
If I want to change the image within one of my UIImageViews within the array
I can use:
[self.guess replaceObjectAtIndex:1 withObject:Square];
but I then have to remove the original view, then add the new subview again:
[[self.guess objectAtIndex:1] removeFromSuperview];
[self.view addSubview:[self.guess objectAtIndex:1]];
The trouble is, my new subview does not inherit the frame position and size
of the original image (as this may have changed). Is there a way to do this
more easily?
I was hoping for something like this, that would just update the original
image in the same position, with the same size:
[self.guess.image replaceObjectAtIndex:1 withObject:Square.image];
Thanks again!

Would it be possible to store only the images in the array and just have one UIImageView? Then, just replace the image inside the UIImageView element

What Radu already said, its better to keep an array of images, or imageNames. Then you also do not have to remove the imageView from the superView, you just reuse the one and only one ImageView.
So what you left is something like this
yourImageView.image = [self.guess objectAtIndex:1];
And if you only store the names in the array
yourImageView.image = [UIImage imageNamed:[self.guess objectAtIndex:1]];

I don't if you've tried this yet, but this should work.
UIImageView *img = (UIImageView *)[self.guess objectAtIndex:1];
img.image = Your Image;

Related

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

how to add& remove overlay image using gridcell index

here is my code
-(void)gridCell:(GridCell *)gCell didSelectImageAtIndex:(NSInteger)index imagSize:(CGRect )frameSize
{
UIImage *img=[UIImage imageNamed:#"Overlay"];
UIImageView *imgView=[[UIImageView alloc]initWithFrame:frameSize];
imgView.image=img;
[_scrPage addSubview:imgView];
[self.view addSubview:_scrPage];
}
please help me how to remove overlay ..
By "overlay" I assume you mean the mean the UIImageView you added the _srcPage? Have you retained some sort of reference to that view? If so, then all you need to do is [imgView removeFromSuperview]; If not, you need to find out how you can get a reference to that specific view.

Add a UIImageView to a Tableview

I have an asychronous image loader which loads images (JImage) in a UIImageview. I want to display these in a tablecell. Obviously i cant set cell.imageView.image because i dont have an image, i just have a view.
How do i set the UIImageview to the tablecell? cell.backgroundView works though, but that paints over the whole cell.
The JImage code is:
NSURL *theUrl=[NSURL URLWithString:imageURL];
JImage *photoImage=[[JImage alloc] init];
[photoImage initWithImageAtURL:theUrl];
[photoImage setContentMode:UIViewContentModeRedraw];
[photoImage setFrame:CGRectMake(0.0f, 0.0f, 40.0f, 65.0f)];
cell.backgroundView = photoImage;
[photoImage release];
which in the JImage.m:
- (void)connectionDidFinishLoading:(NSURLConnection*)theConnection
{
[self setImage:[UIImage imageWithData: data]];
}
use this code and set frame according to your need.
UIImage *imagearrow = [UIImage imageNamed:#"arrow.png"];
UIImageView *imgarrow=[[UIImageView alloc] initWithFrame:CGRectMake(290, 25, 7, 15)];
imgarrow.image =imagearrow;
[cell addSubview:imgarrow];
[imagearrow release];
You don't need to use JImage if all you want to do is load the image asynchronously. One easy way to do this is with GCD and blocks, like so: https://github.com/ChrisTec/iPhone-Book-CodeSamples/blob/master/Chapter%2013/RealEstateViewer%2013.2.5/RealEstateViewer/ImageTableViewController.m
Here's a sample chapter of the book Objective-C Fundamentals which I've co-authored that explains this code in depth: http://www.manning.com/fairbairn/OCF_sample_ch13.pdf
To sum it up: When the cell is requested, you look if you already have downloaded the image. If you have, you display it. If not, you display a spinner and start an async GCD block that will fetch the image. One that block is done, it will run another block on the main thread that switches the spinner out for the image. That's all.
I think you should take a different approach to this. Instead of putting your JImage in the cell, hold it somewhere else in the view controller, an NSArray is usually convenient. Then populate the cells with either placeholder images or just leave them empty.
So let's say you have an array with all the JImages that are loading, when the JImage in the position x of the array finishes loading, you reload that specific cell as it follows:
NSIndexPath indexPath = [NSIndexPath indexPathForRow:x inSection:0];
[self.tableView reloadRowsAtIndexPaths:x withRowAnimation:NO];
And in your cellForRow:AtIndexPath you just have to take the image from the JImage object held in the array.
I hope this helps you

Image VIew problem

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

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