I'm not sure if I'm doing this correctly, but essentially I want to instantiate my uiimageviews when the controller loads. Afterwards, when I implement a method that executes on a timer, I want to grab the reference of the uiimageview depending on the index and add it to the view.
I'm using a NSMutableDictionary. I have all the code in the same method just to test it out. Shouldn't [self.view addSubview:poster]; place the first image in the current view?
self.images = [NSMutableDictionary dictionary];
CGRect imageFrame = CGRectMake(0.0, 0.0, 1024.0, 280.0);
UIImageView *image_one = [[UIImageView alloc] initWithFrame:imageFrame];
image_one.image = [UIImage imageNamed:#"image_one.png"];
NSString *theKey = [NSString stringWithFormat:#"%d",0];
[self.images setObject:image_one forKey:theKey];
UIImageView *image_two = [[UIImageView alloc] initWithFrame:imageFrame];
image_two.image = [UIImage imageNamed:#"image_two.png"];
NSString *theKey1 = [NSString stringWithFormat:#"%d",1];
[self.images setObject:image_two forKey:theKey1];
UIImageView *poster = (UIImageView *)[self.images objectForKey:0];
[self.view addSubview:poster];
First, I would suggest using an NSMutableArray. Since you are referencing the items in the array by an index anyways, might as well use an array.
Although with this code, there are a couple things you should change. You should allocate the NSMutableArray instead of using the autoreleased version.
When you are accessing the UIImageView, the key is a string, not an integer. So, it should read:
UIImageView *poster = (UIImageView *)[self.images objectForKey:#"0"];
Related
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:...];
I parse an XML in my blog app. When I parse it, I have created an object called entry, with properties for Title, Article, Link, Content, and Image. I then have a mutable array called entries that after parsing is finished, I add to entries the object entry. This way, to get the Title of a certain article in a tableview, I can call
RSSEntry *entry = [_Entries objectAtIndex:indexPath.row];
NSString *title = entry.Title;
What I would like to do is have a UIImageView above the tableview, and set that to scroll through all the different UIImages in the array, but am unsure of how to accomplish this. I know of the animationImages property, but not sure what to set as the NSArray in all this, since I use Mutable Array with properties. Any suggestions would greatly help.
There are two options:
a) collect all the images in the separate array by iterating through _Entries to set the animationImages array value;
b) prepare your own view, initialize the timer at constructor with [NSTimer scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:], change the displayed image at selector. To make a smooth animation you can use two imageView subviews setting animated the displayed imageView alpha from 1 to 0 and the next imageView alpha from 0 to 1. As the datasource you can use array of RSSEntry* taking the images from there with a known keypath.
The first option is fairly easier.
I hope you know how to put the UIImageView above the tableview in the xob/storyboard.
Than I am not sure what would you like to do with array of images, but if that mets your needs than it can be a swipe recognizer, and swipe the images
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UISwipeGestureRecognizer* recognizerLeft = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeLeft)];
recognizerLeft.direction =UISwipeGestureRecognizerDirectionLeft;
recognizerLeft.numberOfTouchesRequired =1;
[self.view addGestureRecognizer:recognizerLeft];
[recognizerLeft release];
UISwipeGestureRecognizer* recognizerRight = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:#selector(swipeRight)];
recognizerRight.direction =UISwipeGestureRecognizerDirectionRight;
recognizerRight.numberOfTouchesRequired =1;
[self.view addGestureRecognizer:recognizerRight];
[recognizerRight release];
}
- (IBAction)swipeLeft{
//NSLog(#"Swipe left");
if(pager.currentPage < pager.numberOfPages - 1){
pager.currentPage = (pager.currentPage + 1 );
[self pagerValueChanged];
}
}
- (IBAction)pagerValueChanged
{
NSData* imgData = [images objectAtIndex:pager.currentPage];
UIImage* img = [[UIImage alloc]initWithData:imgData];
imageView.image = img;
//NSLog(#"img width: %f, img height: %f", img.size.width, img.size.height);
[img release];
}
I have paging control and other controls too in my code, but you probably don't need those
This is pretty easy using array enumeration. The following code will take a scroll view that you have already configured and add image views to it for all the objects in your array. It will also dynamically adjust the content size of the scroll view based on the count of the array.
[myMutableArray enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop){
UIImageView *myImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[myMutableArray objectAtIndex:idx]]];
[myImageView setFrame:CGRectMake(myScrollView.frame.size.width * idx, myScrollView.frame.origin.y, myScrollView.frame.size.width, myScrollView.frame.size.height)];
[myScrollView addSubview:myImageView];
[myScrollView setContentSize:CGSizeMake((myScrollView.frame.size.width * idx) + myScrollView.frame.size.width, myScrollView.frame.size.height)];
}];
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;
In my application,i created an imageview on the above the tabbar.I want to display some images here for displaying some adds there.
What my actual problem is...i added this on my table view and whenever i am scrolling the table view ,my imageview is also scrolling.Please help me in this
Thanks in advance.Here is my code
UIImageView *currentLocationImageView = [[UIImageView alloc] init];
NSURL *url1 = [NSURL URLWithString:#"http://imgsrv.995themountain.com/image/kqmt2/UserFiles/Image/SiteGraphics/MTNVideos360x80.jpg"];
UIImage *img1 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url1]];
NSURL *url2 = [NSURL URLWithString:#"http://download.xbox.com/content/images/35f6c527-fb73-40d3-bcb9-bdea2680bc03/1033/banner.png"];
UIImage *img2 = [UIImage imageWithData: [NSData dataWithContentsOfURL:url2]];
NSArray *images = [NSArray arrayWithObjects:img1, img2, nil];
[currentLocationImageView setAnimationImages:images];
[currentLocationImageView setAnimationRepeatCount:0];
[currentLocationImageView setAnimationDuration:5.0];
currentLocationImageView.frame = CGRectMake(0.0, 340.0, 320.0, 30.0);
//self.tableView.tableFooterView = currentLocationImageView;
[currentLocationImageView startAnimating];
[self.view addSubview:currentLocationImageView];
Without seeing more code, it looks as though you may be using a UITableViewController instead of a UIViewController. The difference between the two is important in the case of your last line [self.view addSubview:currentLocationImageView]. What that does in a UIViewController is adds it to the view that would be containing the tableview and the imageview. However, in a UITableViewController the self.view property holds the tableview itself, therefor, it ads your image view as a subview of the tableview, subjecting it to the tableview's scrolling behaviour.
What you can do is change from using a UITableViewController (probably going to be trivial for your application, but may be less than trivial depending on why you opted to use it in the first place); and you'll also need to explicitly create the tableview, and add it to the backing view of the UIViewController subclass you're writing—akin to how you're adding the imageview above.
Hope this helps.
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];