Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Mockup & CodeGen, iPhone & iPad
($9.99)

Make your own iPhone apps
and run them live!
(free)

Manu
($0.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 07-08-2009, 06:00 PM   #1 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Exclamation Images Loading in Wrong Spot

Hello we have been working with an array to get a random image to display on the screen when a button is pressed.
This works, and everytime the button is pressed, a random image from our set is displayed.
However, the image always seems to display in the top left corner of the view, where we are trying to get it in the center of the screen.
We are trying to connect it up with a UIImage view, which is connected along with the view itself to a second view controller- whether that is what we are supposed to do or not, we dont know haha.
So if there is a bit of code to center the displayed image or place it IN a UIImage view, we would appreciate help.

Here is our code when the button is clicked:
Quote:
(IBAction)startClickid)sender
{
[start removeFromSuperview];

myArray = [[NSMutableArray alloc] init];
[myArray addObject:[UIImage imageNamed:@"1.png"]];
[myArray addObject:[UIImage imageNamed:@"2.png"]];
[myArray addObject:[UIImage imageNamed:@"0.png"]];

start = [[UIImageView alloc] initWithImage:[myArray objectAtIndexarc4random() % [myArray count])]];
[self.view addSubview:start];
[start release];

}
also, as a bonus, would anyone be able to tell us how to make it so the randomized images are not displayed twice.
(so it would show 1,3,4,2 or somthing like that as opposed to 1,1,1,2,2,4,3,2, etc.)
Samalope is offline   Reply With Quote
Old 07-08-2009, 10:46 PM   #2 (permalink)
.38 special tucked
 
Join Date: Apr 2009
Location: Brooklyn, NY
Posts: 133
Default

Can you resize the ImageView in IB so that it lives in the center of the screen?
Have you tried that? Can you include a screen shot of your IB layout?
mr-sk is offline   Reply With Quote
Old 07-08-2009, 11:31 PM   #3 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

You should be able to set the position of your view with something like this:

Code:
start.center.x=320/2;
start.center.y=460/2;
UIView Class Reference
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-09-2009, 04:33 PM   #4 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Default

Quote:
Originally Posted by smasher View Post
You should be able to set the position of your view with something like this:

Code:
start.center.x=320/2;
start.center.y=460/2;
UIView Class Reference

Hi-
we tried that but it does not work here is a screen shot of IB. It is not loading in the UIImageView Pictured.
Attached Files
File Type: zip Picture 19.zip (94.5 KB, 19 views)
Samalope is offline   Reply With Quote
Old 07-09-2009, 04:44 PM   #5 (permalink)
Senior Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
Default

Ah. The code you posted creates a brand new UIImageView and attaches it to the current view. If you're trying to display the image in an existing UIImageView, do this:

Code:
myArray = [[NSMutableArray alloc] init];
[myArray addObject:[UIImage imageNamed:@"1.png"]];
[myArray addObject:[UIImage imageNamed:@"2.png"]];
[myArray addObject:[UIImage imageNamed:@"0.png"]];

existingImageView.image = [myArray objectAtIndex: arc4random() % [myArray count]];
You should replace "existingImageView" with the name of the IBOutlet connected to your imageview.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 07-09-2009, 04:57 PM   #6 (permalink)
New Member
 
Join Date: Jul 2009
Posts: 10
Default

Quote:
Originally Posted by smasher View Post
Ah. The code you posted creates a brand new UIImageView and attaches it to the current view. If you're trying to display the image in an existing UIImageView, do this:

Code:
myArray = [[NSMutableArray alloc] init];
[myArray addObject:[UIImage imageNamed:@"1.png"]];
[myArray addObject:[UIImage imageNamed:@"2.png"]];
[myArray addObject:[UIImage imageNamed:@"0.png"]];

existingImageView.image = [myArray objectAtIndex: arc4random() % [myArray count]];




You should replace "existingImageView" with the name of the IBOutlet connected to your imageview.
We did not catch that Thank you again for helping us! Oh yeah and we are going to give you a promo code to try our app out when we are done in a few days (if apple will approve)!
Samalope is offline   Reply With Quote
Reply

Bookmarks

Tags
array, image, load

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 256
18 members and 238 guests
2WeeksToGo, @sandris, AdamL, ADY, BrianSlick, Dani77, Dattee, GHuebner, headkaze, mer10, prchn4christ, Rudy, smithdale87, Thompson22, timle8n1, Touchmint, vigu360
Most users ever online was 1,187, 10-11-2011 at 08:09 AM.
» Stats
Members: 158,880
Threads: 89,228
Posts: 380,747
Top Poster: BrianSlick (7,129)
Welcome to our newest member, @sandris
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 12:52 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0