Browse Source

Fix for issue #2572 Crash on AFImageWithDataAtScale when loading image

#2572 - UIImage initWithData is not thread safe. In order to avoid
crash we need to serialise call
Paulo Ferreira 10 years ago
parent
commit
0c15ad0a80
1 changed files with 12 additions and 1 deletions
  1. 12 1
      AFNetworking/AFURLResponseSerialization.m

+ 12 - 1
AFNetworking/AFURLResponseSerialization.m

@@ -529,12 +529,23 @@ static id AFJSONObjectByRemovingKeysWithNullValues(id JSONObject, NSJSONReadingO
 #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
 #import <CoreGraphics/CoreGraphics.h>
 
+NSLock* _AFImageLock = nil;
+@interface ImageLockInitializer : NSObject
+@end
+@implementation ImageLockInitializer : NSObject
++ (void)initialize {
+    _AFImageLock = [[NSLock alloc] init];
+}
+@end
+
 static UIImage * AFImageWithDataAtScale(NSData *data, CGFloat scale) {
+    [_AFImageLock lock];
     UIImage *image = [[UIImage alloc] initWithData:data];
+    [_AFImageLock unlock];
     if (image.images) {
         return image;
     }
-
+    
     return [[UIImage alloc] initWithCGImage:[image CGImage] scale:scale orientation:image.imageOrientation];
 }