Adding an image to a NSObject - Possible?

巧了我就是萌 提交于 2019-12-24 08:58:03

问题


I'm going to have objects, read from a .csv (Comma seperated file) which contains file names, later. Thats why i'm trying to do it like this.

What I would like to accomplish is to have the button get the image from the filename in the csv.

What i've tried so far: The Object:

@interface question : NSObject
{
@private
UIImage *imageName;
}
@property (nonatomic, retain)UIImage *imageName;

(I have tried with NSString aswell as UIImage, neither works like I want them)

ViewController:

- (void)viewDidLoad
{
[super viewDidLoad];
question *question1 = [[question alloc]init];
[question1.imageName initWithContentsOfFile:@"Default.png"];//Will be read from a csv later on, just typing it in manually for a test
testImage = question1.imageName;

}


-(void)imageshow
{
//(UIImage *)testImage;(NSString *)imageNameString; Didnt work
//[self.testImage initWithContentsOfFile:@"%@",questiont.imageName]; - Doesnt work either

[testButton setImage:testImage forState:UIControlStateNormal];


}
-(IBAction)test:(id)sender
{
[self imageshow];
}

Where this code fails is when I press the test button, nothing happens (not crash)

How can I do this properly? (Add an image with a text filename to a NSObject and then show it in a button/image view)


回答1:


   question1.img = [UIImage imageNamed:@"Default.png"]; 
    [testButton setImage:question1.img forState:UIControlStateNormal];

and yes you can try the following for dynamic images

   question1.img = [UIImage imageNamed:[NSString stringWithFormat:"%@",imgName]];

imgName => is the name of the image not the image example: "abc.png" or "123.png" ,etc




回答2:


if your property is named imageName the accessor variable is automaticly named _imageName.

You are using two different variables in your code.

@interface question : NSObject
{
   @private
   UIImage *_imageName;
}
@property (nonatomic, retain)UIImage *imageName;

imageName is not a great name for an property that contains an image.. it would better fit to a string.




回答3:


There are some things to change.

For mere conventional reasons write all class names with an upper case at the beginning. It eases reading, exchange in forums as SO and avoids mistakes. Second name variables either speaking or abstract, preferrably speaking. But to not choose a mislieading name such imageName. There should be a name in that variable, not an image, or maybe the graphic that displays a name. :)

imageWithContentsOfFile expects a fully qualified file name. If "Default.png" is part of your boundle, as I guess by its name, then imageNamed:@"Default.png" would do.

Your setImage looks nearly good. It cannot work of course while your image is (null). However, it is not good business practis accessing the iVar direct at this point. You shoudl use its setter/getter. Use instead:

[self.testButton setImage:self.testImage forState:UIControlStateNormal];

In your case it does not make much of a difference. But similar to the other conventions it helps avoiding errors when ever you mix your code with some code that you found on the web or when you work in a team.



来源:https://stackoverflow.com/questions/15106683/adding-an-image-to-a-nsobject-possible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!