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 11-21-2010, 04:29 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2010
Posts: 4
reisuke is on a distinguished road
Unhappy NSMutableArray = Application Crash

Hello to everybody

I am trying to create an app, and in this app I will need to hold a lot of images for a UIImageView.

Creating different variables for each image I want to load isn't hard, but it's not the right way to load many images.

So I'm trying the following:
Code:
NSMutableArray *images; //global variable in .m file

- (void)viewDidLoad {
images = [NSMutableArray arrayWithCapacity:30];
	for(NSInteger imageNum=0;imageNum<30;imageNum++)
	{
		NSString *pictureName = [[NSString alloc]initWithFormat:@"%d.png",imageNum];
		UIImage *image = [UIImage imageNamed:pictureName];
		[images addObject:image];
	}
But, even though it compiles ok without any errors or warnings, the app crashes when trying to open.

Please, someone tell me what I am doing wrong and how I can fix it.

Thank you.

(also, can someone tell me if e.g. images[5] is the right way to access an element in the array?)
reisuke is offline   Reply With Quote
Old 11-21-2010, 05:15 PM   #2 (permalink)
Registered Member
 
Join Date: Apr 2010
Posts: 651
kapps11 is on a distinguished road
Default

wats the error for the crash? because the only thing that I could think of possibly crashing with this code was if you added too many objects to the array. Maybe dont give it a specific size when you init it, just use [[NSMutableArray alloc] init]; instead. But you are leaking that NSString for the image name btw.

And in response to your uestion about accessing, NO lol. use [array objectAtIndex:]; go read the docs they explain all that stuff
kapps11 is offline   Reply With Quote
Old 11-21-2010, 05:26 PM   #3 (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
Hello to everybody

I am trying to create an app, and in this app I will need to hold a lot of images for a UIImageView.

Creating different variables for each image I want to load isn't hard, but it's not the right way to load many images.

So I'm trying the following:
Code:
NSMutableArray *images; //global variable in .m file

- (void)viewDidLoad {
images = [NSMutableArray arrayWithCapacity:30];
	for(NSInteger imageNum=0;imageNum<30;imageNum++)
	{
		NSString *pictureName = [[NSString alloc]initWithFormat:@"%d.png",imageNum];
		UIImage *image = [UIImage imageNamed:pictureName];
		[images addObject:image];
	}
But, even though it compiles ok without any errors or warnings, the app crashes when trying to open.

Please, someone tell me what I am doing wrong and how I can fix it.

Thank you.

(also, can someone tell me if e.g. images[5] is the right way to access an element in the array?)

What line is it crashing on?

I'll tell you my guess. My guess is that the code you're posting works fine. It's not until later in another method when you try to USE the array that you're crashing. Why? You're not retaining the array.

You should make the array variable a retained property, and change your code to set the property using property notation, like this:

Code:
self.images = [NSMutableArray arrayWithCapacity:30];
That way, the setter for the images will retain the array and it won't get released.
__________________
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
Reply

Bookmarks

Tags
crash, images, nsmutablearray, picture, uiimageview

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: 375
7 members and 368 guests
apatsufas, Kirkout, lzwasyc, MarkC, Sami Gh, SamorodovAlex, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,664
Threads: 94,120
Posts: 402,898
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Leslie80
Powered by vBadvanced CMPS v3.1.0

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