소스 검색

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 년 전
부모
커밋
0c15ad0a80
1개의 변경된 파일12개의 추가작업 그리고 1개의 파일을 삭제
  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)
 #if defined(__IPHONE_OS_VERSION_MIN_REQUIRED)
 #import <CoreGraphics/CoreGraphics.h>
 #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) {
 static UIImage * AFImageWithDataAtScale(NSData *data, CGFloat scale) {
+    [_AFImageLock lock];
     UIImage *image = [[UIImage alloc] initWithData:data];
     UIImage *image = [[UIImage alloc] initWithData:data];
+    [_AFImageLock unlock];
     if (image.images) {
     if (image.images) {
         return image;
         return image;
     }
     }
-
+    
     return [[UIImage alloc] initWithCGImage:[image CGImage] scale:scale orientation:image.imageOrientation];
     return [[UIImage alloc] initWithCGImage:[image CGImage] scale:scale orientation:image.imageOrientation];
 }
 }