开发者

creating a model class with an image

开发者 https://www.devze.com 2023-02-08 11:25 出处:网络
Is there an efficient way to load an image into a model class?The model is开发者_如何学Go something like this:

Is there an efficient way to load an image into a model class? The model is开发者_如何学Go something like this:

#import <Foundation/Foundation.h>
@interface ProjectData : NSObject {

NSString *titleText;
UIImage *photo;
NSString *descriptionText;

}

@property (nonatomic,retain) NSString *titleText;
@property (nonatomic,retain) UIImage *photo;
@property (nonatomic,retain) NSString *descriptionText;
@end

Project is something like a catalog.

I have been loading the strings from webserver using XML from mySQL database. Images are on database. Not quite sure how to proceed with image loading into ProjectData class. NSMutableData? or is there an easier way?

Thanks.


You can try to use UIImageJPEGRepresentation or UIImagePNGRepresentation when to encode (writing) your objects. And just use [UIImage imageWithData:data] when decoding (reading).

Note: If you will be reading and writing back objects often you should use the PNG version. Jpeg will make the image worse and worse

Note 2: Oh yes, if the images are large you shouldn't store them in your db but rather on disk and just store a reference in the db


Instead of subclassing NSObject, you could subclass UIImageView.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号