2010-04-06 115 views
0

UIImageView * one = [[UIImageView alloc] initWithImage:[[UIImage alloc] initWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@“ball1.png”]]];在UIPickerView中添加图片

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]; 

我用上面的代码在UIPickerView中显示图像。但执行此代码时,应用程序崩溃。

任何人都请帮忙。

回答

0

什么错误,你好吗,看看堆栈跟踪,等...

没有这些信息是很难诊断您的问题,但它看起来像你的代码是完全泄漏。我的猜测是你没有遵循适当的内存管理技术,也没有适当地发布一些东西。此外还有一个更简单的方法做你正在尝试做的:

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

确保您的图像添加到您的项目(右键单击>添加>添加现有文件)

退房的CS139P讲座在内存管理上。

W.R.T.您的评论: 当您没有正确保留一个对象然后尝试向其发送消息时,会发生错误类型。

你的代码可能看起来像这样

@interface SomeObject : NSObject{ 
    NSString* aString; 
} 

@implementation SomeObject 

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

东西,如果你不明白为什么失败,你真的需要回去检讨内存管理与iPhone是如何工作的。

请从iTunes U下载并收听CS193P关于内存管理的讲座,它帮助了我,它会帮助你。

+0

我得到了以下错误,请帮助 ***终止应用程序由于未捕获的异常'NSInvalidArgumentException',原因:'*** - [UIImageView isEqualToString:]:无法识别的选择器发送到实例0x4a21df0' – isarathg 2010-04-06 21:03:03

0

我对pickerView有同样的问题,因为你和我已经修复了它。

首先,您试图将图片添加到您的选取器视图中,并且必须覆盖选取器视图的委托方法。

我想你已经使用了以下委派方法从选择器视图图片:

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

请注意,上面的函数返回的NSString对象,但你的选择器视图必须返回的UIImageView。因此,Xcode会通知一个模棱两可的错误: - [UIImageView isEqualToString:]:无法识别的选择器发送到实例0x4a21df0'(UIImageView不等于NSString)

解决方案是,你必须使用另一个委托视图的委托方法:

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

问题解决了! 如果您的情况与我不一样,请详细说明您的问题。 ^^