diff --git a/MP42/MP42Image.h b/MP42/MP42Image.h index 9928589..20e61bd 100644 --- a/MP42/MP42Image.h +++ b/MP42/MP42Image.h @@ -18,7 +18,7 @@ typedef enum MP42TagArtworkType_e { MP42_ART_PNG = 4 } MP42TagArtworkType; -@interface MP42Image : NSObject { +@interface MP42Image : NSObject { NSImage *_image; NSURL *_url; @@ -32,9 +32,9 @@ typedef enum MP42TagArtworkType_e { - (instancetype)initWithData:(NSData *)data type:(NSInteger)type; - (instancetype)initWithBytes:(const void*)bytes length:(NSUInteger)length type:(NSInteger)type; -@property(readonly) NSImage *image; -@property(readonly) NSURL *url; -@property(readonly) NSData *data; -@property(readonly) NSInteger type; +@property(atomic, readonly) NSImage *image; +@property(atomic, readonly) NSURL *url; +@property(atomic, readonly) NSData *data; +@property(atomic, readonly) NSInteger type; @end \ No newline at end of file diff --git a/MP42/MP42Image.m b/MP42/MP42Image.m index f5e6644..2a6f4fe 100644 --- a/MP42/MP42Image.m +++ b/MP42/MP42Image.m @@ -11,6 +11,10 @@ @implementation MP42Image +@synthesize url = _url; +@synthesize data = _data; +@synthesize type = _type; + - (instancetype)initWithURL:(NSURL *)url type:(NSInteger)type { if (self = [super init]) { @@ -49,6 +53,21 @@ return self; } +- (id)copyWithZone:(NSZone *)zone +{ + MP42Image *copy = nil; + + if (_data) { + copy = [[MP42Image alloc] initWithData:[_data copy] type:_type]; + } else if (_image) { + copy = [[MP42Image alloc] initWithImage:[_image copy]]; + } else if (_url) { + copy = [[MP42Image alloc] initWithURL:[_url copy] type:_type]; + } + + return copy; +} + - (NSImage *)imageFromData:(NSData *)data { NSImage *image = nil; @@ -62,11 +81,13 @@ } - (NSData *)data { - if (_data) - return _data; - else if (_url) { - NSError *outError = nil; - _data = [[NSData dataWithContentsOfURL:_url options:NSDataReadingUncached error:&outError] retain]; + @synchronized(self) { + if (_data) + return _data; + else if (_url) { + NSError *outError = nil; + _data = [[NSData dataWithContentsOfURL:_url options:NSDataReadingUncached error:&outError] retain]; + } } return _data; @@ -74,10 +95,12 @@ - (NSImage *)image { - if (_image) - return _image; - else if (self.data) { - _image = [[self imageFromData:_data] retain]; + @synchronized(self) { + if (_image) + return _image; + else if (self.data) { + _image = [[self imageFromData:_data] retain]; + } } return _image; @@ -128,8 +151,4 @@ [super dealloc]; } -@synthesize url = _url; -@synthesize data = _data; -@synthesize type = _type; - -@end \ No newline at end of file +@end