SDWebImage/SDWebImage/SDWebImageCompat.h

100 lines
2.7 KiB
Objective-C

/*
* This file is part of the SDWebImage package.
* (c) Olivier Poitrey <rs@dailymotion.com>
* (c) Jamie Pinkham
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
#import <TargetConditionals.h>
#ifdef __OBJC_GC__
#error SDWebImage does not support Objective-C Garbage Collection
#endif
#if !__has_feature(objc_arc)
#error SDWebImage is ARC only. Either turn on ARC for the project or use -fobjc-arc flag
#endif
#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_5_0
#error SDWebImage doesn't support Deployement Target version < 5.0
#endif
#if !TARGET_OS_IPHONE
#import <AppKit/AppKit.h>
#ifndef UIImage
#define UIImage NSImage
#endif
#ifndef UIImageView
#define UIImageView NSImageView
#endif
#else
#import <UIKit/UIKit.h>
#endif
// @see https://github.com/ccgus/fmdb/commit/aef763eeb64e6fa654e7d121f1df4c16a98d9f4f
#define SDDispatchQueueRelease(q) (dispatch_release(q))
#if TARGET_OS_IPHONE
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
#undef SDDispatchQueueRelease
#define SDDispatchQueueRelease(q)
#undef SDDispatchQueueSetterSementics
#define SDDispatchQueueSetterSementics strong
#endif
#else
#if MAC_OS_X_VERSION_MIN_REQUIRED >= 1080
#undef SDDispatchQueueRelease
#define SDDispatchQueueRelease(q)
#undef SDDispatchQueueSetterSementics
#define SDDispatchQueueSetterSementics strong
#endif
#endif
#if OS_OBJECT_USE_OBJC
#define SDDispatchQueueSetterSementics strong
#else
#define SDDispatchQueueSetterSementics assign
#endif
NS_INLINE UIImage *SDScaledImageForPath(NSString *path, NSObject *imageOrData)
{
if (!imageOrData)
{
return nil;
}
UIImage *image = nil;
if ([imageOrData isKindOfClass:[NSData class]])
{
image = [[UIImage alloc] initWithData:(NSData *)imageOrData];
}
else if ([imageOrData isKindOfClass:[UIImage class]])
{
image = (UIImage *)imageOrData;
}
else
{
return nil;
}
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
{
CGFloat scale = 1.0;
if (path.length >= 8)
{
// Search @2x. at the end of the string, before a 3 to 4 extension length (only if key len is 8 or more @2x. + 4 len ext)
NSRange range = [path rangeOfString:@"@2x." options:0 range:NSMakeRange(path.length - 8, 5)];
if (range.location != NSNotFound)
{
scale = 2.0;
}
}
UIImage *scaledImage = [[UIImage alloc] initWithCGImage:image.CGImage scale:scale orientation:image.imageOrientation];
image = scaledImage;
}
return image;
}