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
+38 -19

No files matched your search

+5 -5
View File
@@ -18,7 +18,7 @@ typedef enum MP42TagArtworkType_e {
MP42_ART_PNG = 4
} MP42TagArtworkType;
@interface MP42Image : NSObject <NSCoding> {
@interface MP42Image : NSObject <NSCoding, NSCopying> {
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
+33 -14
View File
@@ -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
@end