0
votes

UIImageView *one = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ball1.png"]]] ;

UIImageView *two = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ball2.png"]]] ;
UIImageView *three = [[UIImageView alloc]initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"ball3.png"]]] ;

arrayBalls = [[NSArray alloc]initWithObjects:one,two,three,nil];

I used the above code for displaying image in the UIPickerView. But the application is crashing when executing this code.

Anyone please help.

2

2 Answers

0
votes

What errors are you getting, take a look at the stack trace, ect...

Without that information it is hard to diagnose your problem, however it looks like your code is full of leaks. My guess is that you are not following proper memory management techniques and are not releasing things approprately. Also there is an easier way to do what you are trying to do:

UIImageView *one = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"ball1.png"]]
...
NSArray* ballsArray = [NSArray arrayWithObjects:one,two,three,nil];
[one release];
[two release];
[three release];

Make sure you add the images to your project (right click > add > add existing files)

Check out the CS139P lecture on memory management.

W.R.T. your comment: The type of error you are getting happens when you have not properly retained an object and then try to send it a message.

You're code probably looks something like this

@interface SomeObject : NSObject{
   NSString* aString;
}

@implementation SomeObject

-(void)someMethod{
    aString = [NSString stringWithString:@"A String"];
    ...
}
-(void)someOtherMethod{
    [aString isEqualToString:@"A String"];
    ...
}

If you don't understand why this fails, you really need to go back and review how memory management works with the iphone.

Please download and listen to the CS193P Lecture on memory management from iTunes U, it helped me, it will help you.

0
votes

I had the same problem about pickerView as you and I've fixed it.

First of all, you're trying to add the pictures to your picker view, and you have to overwrite the picker view's delegated method.

I guess that you've used the following delegated method to get the pictures from the picker view:

(NSString *)pickerView: (UIPickerView *)pickerView titleForRow: (NSInteger)row forComponent (NSInteger)component

Please notice that the above function returns NSString object, but your picker view has to return UIImageView. Therefore, Xcode will notify an ambiguous error: -[UIImageView isEqualToString:]: unrecognized selector sent to instance 0x4a21df0' (UIImageView is not equal to NSString)

The solution is that you have to use another delegated method of picker view:

(UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view

The problem is solved! If your situation is not the same as me, please clarify your problem in more detail. ^^