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

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

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

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.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 10-26-2010, 06:05 AM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 4
reisuke is on a distinguished road
Question NSArray of UIImage problem

Hello everyone.
I'm trying to create an array of UIImages using the following code:
Code:
NSArray *tutImages = [NSArray arrayWithObjects:
					  [UIImage imageNamed:@"1.png"],
					  [UIImage imageNamed:@"2.png"],
					  nil];
I realize that the array has to be defined within a function, or else the nil produces a "initializer is not a constant" error.

But if it's inside a function, I can't access it on other functions (which I need to do)

How will I go about creating it in a way that is accessible by other functions?
Also, how will I make my ImageView load an object of the array?

Thank you
reisuke is offline   Reply With Quote
Old 10-26-2010, 06:48 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 107
Warp is on a distinguished road
Default

Well, you could do it like this:

Code:
NSArray* images()
{
    static NSArray *tutImages = [[NSArray alloc] initWithObjects:
					  [UIImage imageNamed:@"1.png"],
					  [UIImage imageNamed:@"2.png"],
					  nil];
    return tutImages;
}
and then have all your code call that function. However, the array is never released, which is ugly.

Last edited by Warp; 10-26-2010 at 06:50 AM.
Warp is offline   Reply With Quote
Old 10-26-2010, 07:32 AM   #3 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 4
reisuke is on a distinguished road
Default

I see. Is there any other way of doing this?
reisuke is offline   Reply With Quote
Old 10-26-2010, 07:40 AM   #4 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by reisuke View Post
I see. Is there any other way of doing this?

Make the array an instance variable (or property) of your object.

Then assign a value to it in the init method.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-26-2010, 08:16 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 4
reisuke is on a distinguished road
Default

I don't quite understand what you are trying to say.
I don't have any object for this. I just want to make a simple array of images.
reisuke is offline   Reply With Quote
Old 10-26-2010, 12:49 PM   #6 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by reisuke View Post
I don't quite understand what you are trying to say.
I don't have any object for this. I just want to make a simple array of images.
What is the file where you are trying to create the array of objects? Is it a file like myViewController.m? Is the method inside an @implementation... @end block?

If so, you are writing instance methods for some object. Just about all the code you write in Cocoa is instance methods for different objects in your project.

In order to create something like an array that can be shared between different methods, you have to create the array as an instance variable of the object where you want to use it.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 11-07-2010, 02:56 PM   #7 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 2
luther_stanton is on a distinguished road
Default

Quote:
Originally Posted by Warp View Post
Well, you could do it like this:

Code:
NSArray* images()
{
    static NSArray *tutImages = [[NSArray alloc] initWithObjects:
					  [UIImage imageNamed:@"1.png"],
					  [UIImage imageNamed:@"2.png"],
					  nil];
    return tutImages;
}
and then have all your code call that function. However, the array is never released, which is ugly.
Consider changing the above code to:

Code:
NSArray* images()
{
    static NSArray *tutImages = [[[NSArray alloc] initWithObjects:
					  [UIImage imageNamed:@"1.png"],
					  [UIImage imageNamed:@"2.png"],
					  nil]autorelease];
    return tutImages;
}
This will free the memory through the auto-release pool. If the caller needs to maintain a reference to the returned result they would need to user the retain method.

You could also call the convenience constructor on NSArrray - arrayWithObjects to achieve the same result.
luther_stanton is offline   Reply With Quote
Old 11-07-2010, 07:45 PM   #8 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by Warp View Post
Well, you could do it like this:

Code:
NSArray* images()
{
    static NSArray *tutImages = [[NSArray alloc] initWithObjects:
					  [UIImage imageNamed:@"1.png"],
					  [UIImage imageNamed:@"2.png"],
					  nil];
    return tutImages;
}
and then have all your code call that function. However, the array is never released, which is ugly.
I don't think that'll compile; static variables need to have a constant initializer.

Try this:

Code:
-(NSArray*) images{
    static NSArray *tutImages = nil;
    if(tutImages==nil){
        tutImages = [[NSArray alloc] initWithObjects:
					 [UIImage imageNamed:@"1.png"],
					 [UIImage imageNamed:@"2.png"],
					 nil];
    }
    return tutImages;
}
Warp is right though; the images will never get released this way. If that's a problem, better to make it an instance variable (declared in the .h, set in the init method) like Duncan said.

Do not autorelease the array in this case; that pretty much guarantees it'll be gone by the time you go to use it.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 11-08-2010, 02:12 AM   #9 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 107
Warp is on a distinguished road
Default

Quote:
Originally Posted by luther_stanton View Post
Consider changing the above code to:

Code:
NSArray* images()
{
    static NSArray *tutImages = [[[NSArray alloc] initWithObjects:
					  [UIImage imageNamed:@"1.png"],
					  [UIImage imageNamed:@"2.png"],
					  nil]autorelease];
    return tutImages;
}
This will free the memory through the auto-release pool.
That's a quite heavy way of doing it. Memory allocation is a relatively heavy operation, and the 'imageNamed' method of UIImage is quite a heavy operation. Given that the contents of the array will always be the same, it feels a complete waste of time to allocate all those objects and run all those 'imageNamed' methods every time you need to access the array. It will also potentially worsen memory fragmentation.
Warp is offline   Reply With Quote
Old 11-08-2010, 02:13 AM   #10 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 107
Warp is on a distinguished road
Default

Quote:
Originally Posted by smasher View Post
I don't think that'll compile; static variables need to have a constant initializer.
Change the extension of the source code file from .m to .mm and it will.
Warp is offline   Reply With Quote
Old 11-08-2010, 05:07 AM   #11 (permalink)
Registered Member
 
Join Date: Nov 2010
Posts: 2
luther_stanton is on a distinguished road
Default

Quote:
Originally Posted by Warp View Post
That's a quite heavy way of doing it. Memory allocation is a relatively heavy operation, and the 'imageNamed' method of UIImage is quite a heavy operation. Given that the contents of the array will always be the same, it feels a complete waste of time to allocate all those objects and run all those 'imageNamed' methods every time you need to access the array. It will also potentially worsen memory fragmentation.
Whoops - the static did not register when I looked at the original post. I agree that using the autorelease pool in this case would not be the best approach - thanks for pointing that out.
luther_stanton is offline   Reply With Quote
Reply

Bookmarks

Tags
nsarray, problem, uiimage

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: 331
14 members and 317 guests
7twenty7, chiataytuday, condor304, Creativ, Domele, dreamdash3, laureix68, LEARN2MAKE, mistergreen2011, mottdog, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,660
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, laureix68
Powered by vBadvanced CMPS v3.1.0

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