Make MP42Image thread-safe.

This commit is contained in:
Damiano Galassi committed 2014-02-09 15:24:02 +01:00
1 parent 2e559043eb
commit 9fdc8e8cd0
2 files changed
+37 -18

No files matched your search

+5 -5
View File
@@ -18,7 +18,7 @@ typedef enum MP42TagArtworkType_e {
MP42_ART_PNG = 4 MP42_ART_PNG = 4
} MP42TagArtworkType; } MP42TagArtworkType;
@interface MP42Image : NSObject <NSCoding> { @interface MP42Image : NSObject <NSCoding, NSCopying> {
NSImage *_image; NSImage *_image;
NSURL *_url; NSURL *_url;
@@ -32,9 +32,9 @@ typedef enum MP42TagArtworkType_e {
- (instancetype)initWithData:(NSData *)data type:(NSInteger)type; - (instancetype)initWithData:(NSData *)data type:(NSInteger)type;
- (instancetype)initWithBytes:(const void*)bytes length:(NSUInteger)length type:(NSInteger)type; - (instancetype)initWithBytes:(const void*)bytes length:(NSUInteger)length type:(NSInteger)type;
@property(readonly) NSImage *image; @property(atomic, readonly) NSImage *image;
@property(readonly) NSURL *url; @property(atomic, readonly) NSURL *url;
@property(readonly) NSData *data; @property(atomic, readonly) NSData *data;
@property(readonly) NSInteger type; @property(atomic, readonly) NSInteger type;
@end @end
+32 -13
View File
@@ -11,6 +11,10 @@
@implementation MP42Image @implementation MP42Image
@synthesize url = _url;
@synthesize data = _data;
@synthesize type = _type;
- (instancetype)initWithURL:(NSURL *)url type:(NSInteger)type - (instancetype)initWithURL:(NSURL *)url type:(NSInteger)type
{ {
if (self = [super init]) { if (self = [super init]) {
@@ -49,6 +53,21 @@
return self; 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 *)imageFromData:(NSData *)data
{ {
NSImage *image = nil; NSImage *image = nil;
@@ -62,11 +81,13 @@
} }
- (NSData *)data { - (NSData *)data {
if (_data) @synchronized(self) {
return _data; if (_data)
else if (_url) { return _data;
NSError *outError = nil; else if (_url) {
_data = [[NSData dataWithContentsOfURL:_url options:NSDataReadingUncached error:&outError] retain]; NSError *outError = nil;
_data = [[NSData dataWithContentsOfURL:_url options:NSDataReadingUncached error:&outError] retain];
}
} }
return _data; return _data;
@@ -74,10 +95,12 @@
- (NSImage *)image - (NSImage *)image
{ {
if (_image) @synchronized(self) {
return _image; if (_image)
else if (self.data) { return _image;
_image = [[self imageFromData:_data] retain]; else if (self.data) {
_image = [[self imageFromData:_data] retain];
}
} }
return _image; return _image;
@@ -128,8 +151,4 @@
[super dealloc]; [super dealloc];
} }
@synthesize url = _url;
@synthesize data = _data;
@synthesize type = _type;
@end @end