diff --git a/JMAnimatedImageView.podspec b/JMAnimatedImageView.podspec index ee22eb6..3856443 100644 --- a/JMAnimatedImageView.podspec +++ b/JMAnimatedImageView.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = 'JMAnimatedImageView' - s.version = '0.2.3' + s.version = '0.2.4' s.requires_arc = true s.author = { 'Morissard Jérome' => 'morissardj@gmail.com' @@ -11,7 +11,7 @@ Pod::Spec.new do |s| s.homepage = 'https://github.com/leverdeterre/JMAnimatedImageView' s.source = { :git => 'https://github.com/leverdeterre/JMAnimatedImageView.git', - :tag => "0.2.3" + :tag => "0.2.4" } s.source_files = 'JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/*' end diff --git a/JMAnimatedImageView/JMAnimatedImageView.xcodeproj/project.pbxproj b/JMAnimatedImageView/JMAnimatedImageView.xcodeproj/project.pbxproj index fc890f4..f46b92c 100644 --- a/JMAnimatedImageView/JMAnimatedImageView.xcodeproj/project.pbxproj +++ b/JMAnimatedImageView/JMAnimatedImageView.xcodeproj/project.pbxproj @@ -7,8 +7,6 @@ objects = { /* Begin PBXBuildFile section */ - C2323BDD19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */; }; - C2323BDE19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */ = {isa = PBXBuildFile; fileRef = C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */; }; C24A92DC199F4C0800CFDBE0 /* 0_verge_super_wide.jpg in Resources */ = {isa = PBXBuildFile; fileRef = C24A92D1199F4C0800CFDBE0 /* 0_verge_super_wide.jpg */; }; C24A92DD199F4C0800CFDBE0 /* 10_verge_super_wide.jpg in Resources */ = {isa = PBXBuildFile; fileRef = C24A92D2199F4C0800CFDBE0 /* 10_verge_super_wide.jpg */; }; C24A92DE199F4C0800CFDBE0 /* 1_verge_super_wide.jpg in Resources */ = {isa = PBXBuildFile; fileRef = C24A92D3199F4C0800CFDBE0 /* 1_verge_super_wide.jpg */; }; @@ -140,8 +138,6 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ - C2323BDB19DC94B000C62D22 /* JMAnimatedGifImageView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = JMAnimatedGifImageView.h; sourceTree = ""; }; - C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = JMAnimatedGifImageView.m; sourceTree = ""; }; C24A92D1199F4C0800CFDBE0 /* 0_verge_super_wide.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 0_verge_super_wide.jpg; sourceTree = ""; }; C24A92D2199F4C0800CFDBE0 /* 10_verge_super_wide.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 10_verge_super_wide.jpg; sourceTree = ""; }; C24A92D3199F4C0800CFDBE0 /* 1_verge_super_wide.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 1_verge_super_wide.jpg; sourceTree = ""; }; @@ -535,8 +531,6 @@ C2F4DB6B19BE5C4700F04261 /* JMAmimatedImageViewMacro.h */, C2B753EF199E88A9004BB1C3 /* JMAnimatedImageView.h */, C2B753F0199E88A9004BB1C3 /* JMAnimatedImageView.m */, - C2323BDB19DC94B000C62D22 /* JMAnimatedGifImageView.h */, - C2323BDC19DC94B000C62D22 /* JMAnimatedGifImageView.m */, C2B03DFA19A52D000032B240 /* JMAnimatedImageView+Image.h */, C2B03DFB19A52D000032B240 /* JMAnimatedImageView+Image.m */, C2B753F1199E88A9004BB1C3 /* JMAnimationOperation.h */, @@ -751,7 +745,6 @@ C2B753F9199E88A9004BB1C3 /* UIImage+JM.m in Sources */, C2B753FC199E8AFE004BB1C3 /* JMTableViewController.m in Sources */, C2954D1C19A72C7600494F41 /* UIDevice+iAppInfos.m in Sources */, - C2323BDD19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */, C2954D1B19A72C7600494F41 /* UIApplication+iAppInfos.m in Sources */, C2B03DF919A474190032B240 /* JMGif.m in Sources */, C2954D1919A72C7600494F41 /* JMOMobileProvisionning.m in Sources */, @@ -766,7 +759,6 @@ buildActionMask = 2147483647; files = ( C2B75352199E8802004BB1C3 /* JMAnimatedImageViewTests.m in Sources */, - C2323BDE19DC94B000C62D22 /* JMAnimatedGifImageView.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView-Info.plist b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView-Info.plist index cf21731..72fe0b5 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView-Info.plist +++ b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView-Info.plist @@ -17,11 +17,11 @@ CFBundlePackageType APPL CFBundleShortVersionString - 0.2.3 + 0.2.4 CFBundleSignature ???? CFBundleVersion - 0.2.3 + 0.2.4 LSRequiresIPhoneOS UIMainStoryboardFile diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedGifImageView.h b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedGifImageView.h deleted file mode 100644 index cb54008..0000000 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedGifImageView.h +++ /dev/null @@ -1,38 +0,0 @@ -// -// JMAnimatedGifImageView.h -// JMAnimatedImageView -// -// Created by jerome morissard on 01/10/14. -// Copyright (c) 2014 jerome morissard. All rights reserved. -// - -#import "JMAnimatedImageView.h" -#import "JMGif.h" - -@interface JMAnimatedGifImageView : JMAnimatedImageView - -//Specific to GIF -@property (strong, readonly, nonatomic) JMGif *gifObject; - -/** - * isAGifImageView - * - * @return BOOL - */ -- (BOOL)isAGifImageView; - -/** - * reloadAnimationImagesFromGifData:, This method reload a GIF image from a GIF NSData - * - * @param data NSData data - */ -- (void)reloadAnimationImagesFromGifData:(NSData *)data; - -/** - * reloadAnimationImagesFromGifNamed:, This method reload a GIF image from a GIF named - * - * @param gitName NSString gitName - */ -- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName; - -@end diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedGifImageView.m b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedGifImageView.m deleted file mode 100644 index c085af9..0000000 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedGifImageView.m +++ /dev/null @@ -1,138 +0,0 @@ -// -// JMAnimatedGifImageView.m -// JMAnimatedImageView -// -// Created by jerome morissard on 01/10/14. -// Copyright (c) 2014 jerome morissard. All rights reserved. -// - -#import "JMAnimatedGifImageView.h" -#import "JMAnimatedLog.h" -#import "JMAnimationOperation.h" - -@implementation JMAnimatedGifImageView - -- (BOOL)isAGifImageView -{ - if (self.gifObject) { - return YES; - } - return NO; -} - -- (void)reloadAnimationImagesFromGifData:(NSData *)data -{ - _gifObject = [[JMGif alloc] initWithData:data]; - self.animationDuration = JMDefaultGifDuration; - [self setCurrentIndex:0]; -} - -- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName -{ - _gifObject = [JMGif gifNamed:gitName]; - self.animationDuration = JMDefaultGifDuration; - [self setCurrentIndex:0]; - [self updateGestures]; -} - -- (void)moveCurrentCardImageFromIndex:(NSInteger)fromIndex - shift:(NSInteger)shift - withDuration:(NSTimeInterval)duration - animationOption:(UIImageViewAnimationOption)option - withCompletionBlock:(JMCompletionFinishBlock)finishBlock -{ - dispatch_async(self.animationManagementQueue, ^{ - - NSTimeInterval unitDuration; - NSInteger shiftUnit = shift / abs((int)shift); // 1 ou -1 - - if (duration == JMDefaultGifDuration) { - unitDuration = duration; - - } else { - if (option == UIImageViewAnimationOptionLinear) { - unitDuration = duration / abs((int)shift); - } else { - unitDuration = duration / abs((int)(shift * shift)); - } - } - - JMLog(@"%s fromIndex:%d shift:%d duration:%lf",__FUNCTION__,(int)fromIndex,(int)shift,duration); - - //[self cancelAnimations]; - [self.animationQueue cancelAllOperations]; - [self.animationQueue waitUntilAllOperationsAreFinished]; - - for (int i = 0; i < (int)abs((int)shift) ; i++) { - - NSInteger index = [self realIndexForComputedIndex:fromIndex+i*shiftUnit]; - JMGifItem *item = [[self.gifObject items] objectAtIndex:index]; - - __weak JMAnimatedGifImageView *weaSelf = self; - JMAnimationOperation *operation = [JMAnimationOperation animationOperationWithDuration:item.delayDuration - completion:^(BOOL finished) - { - - if (weaSelf.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) { - if ([weaSelf operationQueueIsFinished] == YES) { - if (finishBlock) { - finishBlock(YES); - } - - if (weaSelf.animationRepeatCount == 0 && - weaSelf.animationState == UIImageViewAnimationStateInPgrogress) { - [weaSelf continueAnimating]; - } - } - } - }]; - - operation.animatedImageView = self; - operation.imageIndex = index; - [self.animationQueue addOperation:operation]; - } - }); -} - -- (void)startAnimating -{ - self.animationState = UIImageViewAnimationStateInPgrogress; - - if ([self checkLifeCycleSanity] == NO) { - return; - } - - if (self.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) { - [self moveCurrentCardImageFromIndex:self.currentIndex - shift:self.gifObject.items.count - withDuration:self.animationDuration - animationOption:UIImageViewAnimationOptionLinear - withCompletionBlock:NULL]; - - } else if (self.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinear) { - [self changeImageToIndex:(self.currentIndex + 1) withTimeInterval:self.animationDuration repeat:YES]; - } -} - -- (void)continueAnimating -{ - self.animationState = UIImageViewAnimationStateInPgrogress; - - dispatch_async(dispatch_get_main_queue(), ^{ - if ([self checkLifeCycleSanity] == NO) { - return; - } - - if ([self operationQueueIsFinished] == NO) { - return; - } - - [self moveCurrentCardImageFromIndex:self.currentIndex - shift:self.gifObject.items.count - withDuration:self.animationDuration - animationOption:UIImageViewAnimationOptionLinear - withCompletionBlock:NULL]; - }); -} - -@end diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView+Image.m b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView+Image.m index 2aaa7f3..96f84af 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView+Image.m +++ b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView+Image.m @@ -8,15 +8,14 @@ #import "JMAnimatedImageView+Image.h" #import "UIImage+JM.h" -#import "JMAnimatedGifImageView.h" +#import "JMGif.h" @implementation JMAnimatedImageView (Image) - (UIImage *)imageAtIndex:(NSInteger)index { - if ([self isKindOfClass:[JMAnimatedGifImageView class]]) { - JMAnimatedGifImageView *gifView = (JMAnimatedGifImageView *)self; - return [gifView.gifObject imageAtIndex:index]; + if ([self isAGifImageView]) { + return [self.gifObject imageAtIndex:index]; } else if ([self.animationDatasource respondsToSelector:@selector(imageAtIndex:forAnimatedImageView:)]) { return [self.animationDatasource imageAtIndex:index forAnimatedImageView:self]; @@ -32,10 +31,8 @@ - (UIImage *)imageAtIndex:(NSInteger)index - (NSUInteger)numberOfImages { - if ([self isKindOfClass:[JMAnimatedGifImageView class]]) { - JMAnimatedGifImageView *gifView = (JMAnimatedGifImageView *)self; - return gifView.gifObject.items.count; - + if ([self isAGifImageView]) { + return self.gifObject.items.count; } else { if ([self.animationDatasource respondsToSelector:@selector(numberOfImagesForAnimatedImageView:)]) { return [self.animationDatasource numberOfImagesForAnimatedImageView:self]; diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.h b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.h index 03fa693..278c25b 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.h +++ b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.h @@ -43,6 +43,7 @@ typedef NS_ENUM(NSUInteger, UIImageViewAnimationState) { typedef void (^JMCompletionFinishBlock)(BOOL resul); +@class JMGif; @interface JMAnimatedImageView : UIImageView @property (weak, nonatomic) IBOutlet id animationDatasource; @@ -85,6 +86,7 @@ typedef void (^JMCompletionFinishBlock)(BOOL resul); */ - (void)animateToIndex:(NSInteger)index withDuration:(NSTimeInterval)duration; + /** * animateToIndex:withDuration:withCompletionBlock:, This method will animate the modification of images to access to the index in parameter. * @@ -94,10 +96,29 @@ typedef void (^JMCompletionFinishBlock)(BOOL resul); */ - (void)animateToIndex:(NSInteger)index withDuration:(NSTimeInterval)duration withCompletionBlock:(JMCompletionFinishBlock)finishBlock; -- (void)updateGestures; -- (void)changeImageToIndex:(NSInteger)index withTimeInterval:(NSTimeInterval)duration repeat:(BOOL)repeat; -- (BOOL)operationQueueIsFinished; -- (BOOL)checkLifeCycleSanity; -- (NSInteger)realIndexForComputedIndex:(NSInteger)index; +//Specific to GIF +@property (strong, readonly, nonatomic) JMGif *gifObject; + +/** + * isAGifImageView + * + * @return BOOL + */ +- (BOOL)isAGifImageView; + +/** + * reloadAnimationImagesFromGifData:, This method reload a GIF image from a GIF NSData + * + * @param data NSData data + */ +- (void)reloadAnimationImagesFromGifData:(NSData *)data; +- (void)reloadAnimationImagesFromGifData:(NSData *)data fromUrl:(NSURL *)url; + +/** + * reloadAnimationImagesFromGifNamed:, This method reload a GIF image from a GIF named + * + * @param gitName NSString gitName + */ +- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName; @end diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.m b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.m index 8116f48..f951777 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.m +++ b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView.m @@ -11,6 +11,7 @@ #import "UIImage+JM.h" #import "JMAnimatedImageView+Image.h" #import "JMAnimatedLog.h" +#import "JMGif.h" @interface JMAnimatedImageView() @property (nonatomic, assign) NSInteger operationInQueue; @@ -67,6 +68,28 @@ - (void)reloadAnimationImages } } +- (void)reloadAnimationImagesFromGifData:(NSData *)data fromUrl:(NSURL *)url +{ + _gifObject = [[JMGif alloc] initWithData:data fromURL:url]; + self.animationDuration = JMDefaultGifDuration; + [self setCurrentCardImageAtindex:0]; +} + +- (void)reloadAnimationImagesFromGifData:(NSData *)data +{ + _gifObject = [[JMGif alloc] initWithData:data]; + self.animationDuration = JMDefaultGifDuration; + [self setCurrentCardImageAtindex:0]; +} + +- (void)reloadAnimationImagesFromGifNamed:(NSString *)gitName +{ + _gifObject = [JMGif gifNamed:gitName]; + self.animationDuration = JMDefaultGifDuration; + [self setCurrentCardImageAtindex:0]; + [self updateGestures]; +} + - (BOOL)checkLifeCycleSanity { if (self.superview) { @@ -118,6 +141,14 @@ - (void)setupTempImageViewAtOriginiX:(CGFloat)x #pragma mark - overided setter +- (BOOL)isAGifImageView +{ + if (_gifObject) { + return YES; + } + return NO; +} + - (void)setInteractiveAnimation:(BOOL)interactiveAnimation { _interactiveAnimation = interactiveAnimation; @@ -271,7 +302,7 @@ - (void)imageViewTouchedWithPanGesture:(UIPanGestureRecognizer *)gestureRecogniz NSInteger pointUnity = self.frame.size.width / [self numberOfImages]; //Compute inerty using velocity - NSInteger shift = abs(velocity.x) / (16 * [UIScreen mainScreen].scale * pointUnity); + NSInteger shift = fabs(velocity.x) / (16 * [UIScreen mainScreen].scale * pointUnity); if(velocity.x > 0) { [self setCurrentIndex:index+(_imageOrder) * shift]; @@ -312,6 +343,28 @@ - (void)imageViewTouched:(UIGestureRecognizer *)gestureRecognizer #pragma mark - Load images +- (void)setCurrentCardImageAtindex:(NSInteger)index +{ + NSInteger realIndex = [self realIndexForComputedIndex:index]; + self.image = [self imageAtIndex:realIndex]; + self.currentIndex = realIndex; + + if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) { + [self updatePageControl]; + } +} + +- (void)setCurrentImage:(UIImage *)img forIndex:(NSInteger)index +{ + JMLog(@"%s index:%d",__FUNCTION__,(int)index); + NSInteger realIndex = [self realIndexForComputedIndex:index]; + self.image = img; + self.currentIndex = realIndex; + if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) { + [self updatePageControl]; + } +} + - (NSInteger)realIndexForComputedIndex:(NSInteger)index { NSInteger nb = [self numberOfImages]; @@ -371,27 +424,30 @@ - (void)moveCurrentCardImageFromIndex:(NSInteger)fromIndex } else { NSInteger index = [self realIndexForComputedIndex:fromIndex+i*shiftUnit]; - __weak JMAnimatedImageView *weaSelf = self; - + if ([self isAGifImageView]) { + JMGifItem *item = [[self.gifObject items] objectAtIndex:index]; + currentInterval = [item delayDuration]; + } + + __weak JMAnimatedImageView *weakSelf = self; JMAnimationOperation *operation = [JMAnimationOperation animationOperationWithDuration:currentInterval completion:^(BOOL finished) { - if (weaSelf.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) { - if ([weaSelf operationQueueIsFinished] == YES) { + if (weakSelf.animationType == JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition) { + if ([weakSelf operationQueueIsFinished] == YES) { if (finishBlock) { finishBlock(YES); } - if (weaSelf.animationRepeatCount == 0 && - weaSelf.animationState == UIImageViewAnimationStateInPgrogress) { - [weaSelf continueAnimating]; + if (weakSelf.animationRepeatCount == 0 && weakSelf.animationState == UIImageViewAnimationStateInPgrogress) { + [weakSelf continueAnimating]; } } } }]; - operation.animatedImageView = self; + operation.animatedImageView = weakSelf; operation.imageIndex = index; currentInterval = 0; diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.h b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.h index 218260c..c3f1244 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.h +++ b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.h @@ -8,9 +8,12 @@ #import @interface JMGif : NSObject + @property (readonly, nonatomic) NSArray *items; //images + infos +@property (readonly, nonatomic) NSURL *url; - (instancetype)initWithData:(NSData *)data; +- (instancetype)initWithData:(NSData *)data fromURL:(NSURL *)url; + (instancetype)gifNamed:(NSString *)gifName; - (UIImage *)imageAtIndex:(NSInteger)index; @@ -19,6 +22,7 @@ + (BOOL)cleanGifCacheError:(NSError **)error; + (BOOL)cleanGifCacheForGifNamed:(NSString *)gifName; + (BOOL)cleanGifCacheForGifNamed:(NSString *)gifName error:(NSError **)error; ++ (void)cacheGifData:(NSData *)data gifAbsoluteUrl:(NSString *)absoluteUrl; @end diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.m b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.m index 160cd5b..7745207 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.m +++ b/JMAnimatedImageView/JMAnimatedImageView/JMAnimatedImageView/JMGif.m @@ -19,18 +19,27 @@ @interface JMGif () @implementation JMGif -- (instancetype)initWithData:(NSData *)data gifName:(NSString *)gifName +- (instancetype)initWithData:(NSData *)data gifName:(NSString *)gifName fromURL:(NSURL *)url { self = [super init]; if (self) { - [self loadGifWithData:data gifName:gifName]; + if (url.absoluteString.length && data) { + [self.class cacheGifData:data gifAbsoluteUrl:url.absoluteString]; + } + + [self loadGifWithData:data gifName:gifName fromURL:url]; } return self; } - (instancetype)initWithData:(NSData *)data { - return [self initWithData:data gifName:nil]; + return [self initWithData:data gifName:nil fromURL:nil]; +} + +- (instancetype)initWithData:(NSData *)data fromURL:(NSURL *)url +{ + return [self initWithData:data gifName:nil fromURL:url]; } + (instancetype)gifNamed:(NSString *)gifName @@ -39,14 +48,54 @@ + (instancetype)gifNamed:(NSString *)gifName NSURL *url = [[NSBundle mainBundle] URLForResource:gifName withExtension:@"gif"]; NSData *data = [NSData dataWithContentsOfURL:url options:0 error:&error]; if (nil == error) { - JMGif *gif = [[JMGif alloc] initWithData:data gifName:gifName]; + JMGif *gif = [[JMGif alloc] initWithData:data gifName:gifName fromURL:nil]; return gif; } return nil; } -- (void)loadGifWithData:(NSData *)data gifName:(NSString *)gifName ++ (void)cacheGifData:(NSData *)data gifAbsoluteUrl:(NSString *)absoluteUrl +{ + CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); + + if (!imageSource) { + JMOLog(@"Error: Failed to `CGImageSourceCreateWithData` for animated GIF data %@", data); + return; + } + + // Early return if not GIF! + CFStringRef imageSourceContainerType = CGImageSourceGetType(imageSource); + if (!UTTypeConformsTo(imageSourceContainerType, kUTTypeGIF)) { + JMOLog(@"Error: Supplied data is of type %@ and doesn't seem to be GIF data %@", imageSourceContainerType, data); + CFRelease(imageSource); + return; + } + + // Iterate through frame images + size_t imageCount = CGImageSourceGetCount(imageSource); + for (size_t i = 0; i < imageCount; i++) { + CGImageRef frameImageRef = CGImageSourceCreateImageAtIndex(imageSource, i, NULL); + if (frameImageRef) { + //UIImage *frameImage = [UIImage imageWithCGImage:frameImageRef]; + // Check for valid `frameImage` before parsing its properties as frames can be corrupted (and `frameImage` even `nil` when `frameImageRef` was valid). + if (YES) { + NSDictionary *frameProperties = (__bridge_transfer NSDictionary *)CGImageSourceCopyPropertiesAtIndex(imageSource, i, NULL); + NSDictionary *framePropertiesGIF = [frameProperties objectForKey:(id)kCGImagePropertyGIFDictionary]; + if (absoluteUrl) { + if ([self gifNamedAlreadyCached:absoluteUrl index:i] == NO) { + [self cacheGifName:absoluteUrl image:[UIImage imageWithCGImage:frameImageRef] representingIndex:i]; + } + } + } + + CFRelease(frameImageRef); + } + } + CFRelease(imageSource); +} + +- (void)loadGifWithData:(NSData *)data gifName:(NSString *)gifName fromURL:(NSURL *)url { CGImageSourceRef imageSource = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); @@ -92,7 +141,16 @@ - (void)loadGifWithData:(NSData *)data gifName:(NSString *)gifName if ([self.class gifNamedAlreadyCached:gifName index:i] == NO) { [self.class cacheGifName:gifName image:[UIImage imageWithCGImage:frameImageRef] representingIndex:i]; } + + } else if (url.absoluteString) { + item = [[JMGifItem alloc] initWithImagePath:[self.class imagePathForeGifName:url.absoluteString index:i] + frameProperties:framePropertiesGIF]; + if ([self.class gifNamedAlreadyCached:url.absoluteString index:i] == NO) { + [self.class cacheGifName:url.absoluteString image:[UIImage imageWithCGImage:frameImageRef] representingIndex:i]; + } + } else { + JMOLog(@"Loading full UIImage ..."); item = [[JMGifItem alloc] initWithImage:[UIImage imageWithCGImage:frameImageRef] frameProperties:framePropertiesGIF]; } diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.m b/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.m index 116821c..ee159ac 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.m +++ b/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.m @@ -7,12 +7,12 @@ // #import "JMFLViewController.h" -#import "JMAnimatedGifImageView.h" +#import "JMAnimatedImageView.h" @interface JMFLViewController () -@property (weak, nonatomic) IBOutlet JMAnimatedGifImageView *carImageView1; -@property (weak, nonatomic) IBOutlet JMAnimatedGifImageView *carImageView2; -@property (weak, nonatomic) IBOutlet JMAnimatedGifImageView *carImageView3; +@property (weak, nonatomic) IBOutlet JMAnimatedImageView *carImageView1; +@property (weak, nonatomic) IBOutlet JMAnimatedImageView *carImageView2; +@property (weak, nonatomic) IBOutlet JMAnimatedImageView *carImageView3; @end @implementation JMFLViewController @@ -23,51 +23,39 @@ - (void)viewDidLoad // Do any additional setup after loading the view from its nib. self.title = @"3 GIF, 3 animations ..."; - /* - JMDemoGIFAutomaticAnimationUsingImageViewImageAndSystemCache, - JMDemoGIFInteractiveAnimationUsingImageViewImageAndSystemCache, - JMDemoGIFMultipleAnimationUsingImageViewImageAndSystemCache - */ - - if (self.demoExemple == JMDemoGIFAutomaticAnimationUsingImageViewImageLowMemoryPressure) { - NSURL *url = [[NSBundle mainBundle] URLForResource:@"rock" withExtension:@"gif"]; - NSData *data = [NSData dataWithContentsOfURL:url]; - self.carImageView1.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView1 reloadAnimationImagesFromGifData:data]; - self.carImageView1.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; - self.carImageView1.animationDuration = 3; - [self.carImageView1 startAnimating]; + + self.carImageView1.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; + [self.carImageView1 reloadAnimationImagesFromGifNamed:@"rock"]; + + self.carImageView2.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; + [self.carImageView2 reloadAnimationImagesFromGifNamed:@"Rotating_earth"]; + + self.carImageView3.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; + [self.carImageView3 reloadAnimationImagesFromGifNamed:@"nyan"]; + + if (self.demoExemple & JMDemoInteractive) { + self.carImageView1.animationType = JMAnimatedImageViewAnimationTypeInteractive; + self.carImageView2.animationType = JMAnimatedImageViewAnimationTypeInteractive; + self.carImageView3.animationType = JMAnimatedImageViewAnimationTypeInteractive; + [self.carImageView1 setInteractiveAnimation:YES]; + [self.carImageView2 setInteractiveAnimation:YES]; + [self.carImageView3 setInteractiveAnimation:YES]; - url = [[NSBundle mainBundle] URLForResource:@"Rotating_earth" withExtension:@"gif"]; - data = [NSData dataWithContentsOfURL:url]; - self.carImageView2.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView2 reloadAnimationImagesFromGifData:data]; + } else { + self.carImageView1.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; self.carImageView2.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; - self.carImageView2.animationDuration = 3; - [self.carImageView2 startAnimating]; - - url = [[NSBundle mainBundle] URLForResource:@"nyan" withExtension:@"gif"]; - data = [NSData dataWithContentsOfURL:url]; - self.carImageView3.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView3 reloadAnimationImagesFromGifData:data]; self.carImageView3.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; - [self.carImageView3 startAnimating]; + [self.carImageView1 setInteractiveAnimation:NO]; + [self.carImageView2 setInteractiveAnimation:NO]; + [self.carImageView3 setInteractiveAnimation:NO]; - } else if (self.demoExemple == JMDemoGIFInteractiveAnimationUsingImageViewImageLowMemoryPressure) { - self.carImageView1.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView1 reloadAnimationImagesFromGifNamed:@"rock"]; - self.carImageView1.animationType = JMAnimatedImageViewAnimationTypeInteractive; - [self.carImageView1 setInteractiveAnimation:YES]; - - self.carImageView2.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView2 reloadAnimationImagesFromGifNamed:@"Rotating_earth"]; - self.carImageView2.animationType = JMAnimatedImageViewAnimationTypeInteractive; - [self.carImageView2 setInteractiveAnimation:YES]; + self.carImageView1.animationDuration = 3; + self.carImageView2.animationDuration = 3; + self.carImageView3.animationDuration = 3; - self.carImageView3.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView3 reloadAnimationImagesFromGifNamed:@"nyan"]; - self.carImageView3.animationType = JMAnimatedImageViewAnimationTypeInteractive; - [self.carImageView3 setInteractiveAnimation:YES]; + [self.carImageView1 startAnimating]; + [self.carImageView2 startAnimating]; + [self.carImageView3 startAnimating]; } } diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.xib b/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.xib index c8f82a9..a30aaf7 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.xib +++ b/JMAnimatedImageView/JMAnimatedImageView/JMFLViewController.xib @@ -1,7 +1,8 @@ - + - + + @@ -17,21 +18,17 @@ - + - - + - - + - - @@ -41,4 +38,9 @@ + + + + + diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.h b/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.h index 541e2a1..9a7ec1c 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.h +++ b/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.h @@ -8,15 +8,15 @@ #import -typedef NS_ENUM(NSUInteger, JMDemoType) { - JMDemoAutomaticAnimationUsingImageViewImageAndSystemCache = 0, - JMDemoAutomaticAnimationUsingJMAnimatedImageViewImageAndSystemCache, - JMDemoAutomaticAnimationUsingJMAnimatedImageViewImageAndWithoutCache, - JMDemoAutoSwipeAnimationUsingJMAnimatedImageViewImageAndWithoutCache, - JMDemoInteractiveAnimationUsingJMAnimatedImageViewImageAndWithoutCache, - JMDemoCarouselUsingJMAnimatedImageViewImageAndWithoutCache, - JMDemoGIFAutomaticAnimationUsingImageViewImageLowMemoryPressure, - JMDemoGIFInteractiveAnimationUsingImageViewImageLowMemoryPressure +typedef NS_OPTIONS(NSUInteger, JMDemoType) { + JMDemoAutomatic = 1, + JMDemoInteractive = 1 << 1, + JMDemoChangeImageSwipeTransition = 1 << 2, + JMDemoChangeImageNoTransition = 1 << 3, + JMDemoMemoryBySystem = 1 << 4, + JMDemoMemoryByMyComponent = 1 << 5, + JMDemoReverseImage = 1 << 6, + JMDemoPhotos = 1 << 8 }; @interface JMTableViewController : UITableViewController diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.m b/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.m index 775d021..48dae25 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.m +++ b/JMAnimatedImageView/JMAnimatedImageView/JMTableViewController.m @@ -50,7 +50,7 @@ - (void)viewDidAppear:(BOOL)animated - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { - return 8; + return 7; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath @@ -58,55 +58,44 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N JMTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"JMTableViewCell" forIndexPath:indexPath]; switch (indexPath.row) { - case JMDemoAutomaticAnimationUsingImageViewImageAndSystemCache: - cell.jmLabel.text = @"AUTOMATIC ANIMATION : using UIImageView"; + case 0: + cell.jmLabel.text = @"PNG + Automatic Transition + system cache"; cell.jmDetailsLabel.text = @"All images are loaded in one time in memory (so ... it's take a lot of time to run the 1st time)."; break; - case JMDemoAutomaticAnimationUsingJMAnimatedImageViewImageAndSystemCache: - cell.jmLabel.text = @"AUTOMATIC ANIMATION : using JMAnimatedImageView (using Sytem cache)"; + case 1: + cell.jmLabel.text = @"PNG + Automatic Transition + system cache"; cell.jmDetailsLabel.text = @"Images are loaded during the animation."; break; - case JMDemoAutomaticAnimationUsingJMAnimatedImageViewImageAndWithoutCache: - cell.jmLabel.text = @"AUTOMATIC ANIMATION : using JMAnimatedImageView (Low memory usage)"; + case 2: + cell.jmLabel.text = @"PNG + Automatic Transition + JMAnimatedImageView cache"; cell.jmDetailsLabel.text = @"More CPU time to load / reload images but less memory used."; break; - case JMDemoAutoSwipeAnimationUsingJMAnimatedImageViewImageAndWithoutCache: - cell.jmLabel.text = @"AUTOMATIC ANIMATION : using JMAnimatedImageView with transition"; - cell.jmDetailsLabel.text = @"More CPU time to load / reload images but less memory used."; - break; - - case JMDemoInteractiveAnimationUsingJMAnimatedImageViewImageAndWithoutCache: - cell.jmLabel.text = @"INTERACTIVE ANIMATION : using JMAnimatedImageView (Low memory usage)"; + case 3: + cell.jmLabel.text = @"PNG + Interactive Transition + JMAnimatedImageView cache"; cell.jmDetailsLabel.text = @"Swipe left / Right to manage the animation."; break; - case JMDemoCarouselUsingJMAnimatedImageViewImageAndWithoutCache: - cell.jmLabel.text = @"SIMPLE CAROUSEL : using JMAnimatedImageView (Low memory usage)"; - cell.jmDetailsLabel.text = @"Swipe left / Right"; + case 4: + cell.jmLabel.text = @"PNG + Carousel Transition + JMAnimatedImageView cache"; + cell.jmDetailsLabel.text = @"Swipe left / Right to swipe"; break; - - case JMDemoGIFAutomaticAnimationUsingImageViewImageLowMemoryPressure: - cell.jmLabel.text = @"GIF ANIMATION : using JMAnimatedImageView (Low memory usage)"; + + case 5: + cell.jmLabel.text = @"GIF + Automatic Transition + JMAnimatedImageView cache"; cell.jmDetailsLabel.text = @"Swipe left / Right"; break; - case JMDemoGIFInteractiveAnimationUsingImageViewImageLowMemoryPressure: - cell.jmLabel.text = @"GIF INTERACTION : using JMAnimatedImageView (Low memory usage)"; + case 6: + cell.jmLabel.text = @"GIF + Interactive Transition + JMAnimatedImageView cache"; cell.jmDetailsLabel.text = @"Swipe left / Right"; break; default: break; } - - /* - cell.jmLabel.text = @"MULTIPLE GIF ANIMATIONS : using JMAnimatedImageView (Low memory usage)"; - cell.jmDetailsLabel.text = @""; - */ - return cell; } @@ -114,18 +103,50 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath { UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil]; - if (indexPath.row == JMDemoGIFAutomaticAnimationUsingImageViewImageLowMemoryPressure || - indexPath.row == JMDemoGIFInteractiveAnimationUsingImageViewImageLowMemoryPressure) - { + JMDemoType demoType = 0; + switch (indexPath.row) { + case 0: + demoType = ( JMDemoAutomatic | JMDemoMemoryBySystem | JMDemoChangeImageNoTransition); + break; + + case 1: + demoType = ( JMDemoAutomatic | JMDemoMemoryBySystem | JMDemoChangeImageNoTransition); + break; + + case 2: + demoType = ( JMDemoAutomatic | JMDemoMemoryByMyComponent | JMDemoChangeImageNoTransition); + break; + + case 3: + demoType = ( JMDemoInteractive | JMDemoMemoryByMyComponent | JMDemoChangeImageNoTransition | JMDemoReverseImage); + break; + + case 4: + demoType = ( JMDemoInteractive | JMDemoMemoryByMyComponent | JMDemoChangeImageSwipeTransition | JMDemoPhotos); + break; + + case 5: + demoType = ( JMDemoAutomatic | JMDemoMemoryByMyComponent | JMDemoChangeImageNoTransition); + break; + + case 6: + demoType = ( JMDemoInteractive | JMDemoMemoryByMyComponent | JMDemoChangeImageNoTransition); + break; + + default: + break; + } + + if (indexPath.row > 4) { JMFLViewController *vc = [JMFLViewController new]; UIViewController *vcToPush = vc; - vc.demoExemple = indexPath.row; + vc.demoExemple = demoType; [self.navigationController pushViewController:vcToPush animated:YES]; } else { JMViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"JMViewController"]; UIViewController *vcToPush = vc; - vc.demoExemple = indexPath.row; + vc.demoExemple = demoType; [self.navigationController pushViewController:vcToPush animated:YES]; } } diff --git a/JMAnimatedImageView/JMAnimatedImageView/JMViewController.m b/JMAnimatedImageView/JMAnimatedImageView/JMViewController.m index 189f908..16b0884 100644 --- a/JMAnimatedImageView/JMAnimatedImageView/JMViewController.m +++ b/JMAnimatedImageView/JMAnimatedImageView/JMViewController.m @@ -24,109 +24,55 @@ - (void)viewDidLoad [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. - switch (self.demoExemple) { - case JMDemoAutomaticAnimationUsingImageViewImageAndSystemCache: - { - self.imageView.hidden = NO; - self.carImageView.hidden = YES; - - NSMutableArray *images = [NSMutableArray new]; - for (int i = 0; i < [self numberOfImagesForAnimatedImageView:self.carImageView]; i++) { - [images addObject:[UIImage imageNamed:[self imageNameAtIndex:i forAnimatedImageView:self.carImageView]]]; - } - - self.imageView.animationImages = images; - self.imageView.animationRepeatCount = 0; - self.imageView.animationDuration = 4.0; - [self.imageView startAnimating]; - } - break; - - case JMDemoAutomaticAnimationUsingJMAnimatedImageViewImageAndSystemCache: - self.imageView.hidden = YES; - self.carImageView.hidden = NO; - self.carImageView.animationDelegate = self; - self.carImageView.animationDatasource = self; - self.carImageView.animationRepeatCount = 0; - self.carImageView.animationDuration = 4.0; - self.carImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; - self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageSystemCache; - self.carImageView.imageOrder = JMAnimatedImageViewOrderNormal; - - self.animationType = self.carImageView.animationType; - - [self.carImageView startAnimating]; - break; - - case JMDemoAutomaticAnimationUsingJMAnimatedImageViewImageAndWithoutCache: - self.imageView.hidden = YES; - self.carImageView.hidden = NO; - self.carImageView.animationDelegate = self; - self.carImageView.animationDatasource = self; - self.carImageView.animationRepeatCount = 0; - self.carImageView.animationDuration = 4.0; + if (self.demoExemple & JMDemoAutomatic) { + if (self.demoExemple & JMDemoChangeImageSwipeTransition) { self.carImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinear; - self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - self.carImageView.imageOrder = JMAnimatedImageViewOrderNormal; - - self.animationType = self.carImageView.animationType; - - [self.carImageView startAnimating]; - break; - - case JMDemoAutoSwipeAnimationUsingJMAnimatedImageViewImageAndWithoutCache: - self.imageView.hidden = YES; - self.carImageView.hidden = NO; - self.carImageView.animationDelegate = self; - self.carImageView.animationDatasource = self; + + } else { self.carImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; - self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - self.carImageView.imageOrder = JMAnimatedImageViewOrderNormal; - self.carImageView.animationDuration = 5.0; - self.animationType = self.carImageView.animationType; - - [self.carImageView startAnimating]; - break; - - case JMDemoInteractiveAnimationUsingJMAnimatedImageViewImageAndWithoutCache: - self.imageView.hidden = YES; - self.carImageView.hidden = NO; - self.carImageView.animationDelegate = self; - self.carImageView.animationDatasource = self; - self.carImageView.animationType = JMAnimatedImageViewAnimationTypeInteractive; - self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - self.carImageView.imageOrder = JMAnimatedImageViewOrderReverse; - self.animationType = self.carImageView.animationType; - - [self.carImageView reloadAnimationImages]; - [self.carImageView setInteractiveAnimation:YES]; - break; - - case JMDemoCarouselUsingJMAnimatedImageViewImageAndWithoutCache: - self.imageView.hidden = YES; - self.carImageView.hidden = NO; - self.carImageView.animationDelegate = self; - self.carImageView.animationDatasource = self; + } + + } + + if (self.demoExemple & JMDemoInteractive) { + self.carImageView.animationType = JMAnimatedImageViewAnimationTypeInteractive; + + if (self.demoExemple & JMDemoChangeImageSwipeTransition) { self.carImageView.animationType = JMAnimatedImageViewAnimationTypeManualSwipe; - self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - self.carImageView.imageOrder = JMAnimatedImageViewOrderNormal; - - self.animationType = self.carImageView.animationType; + } + } + + if (self.demoExemple & JMDemoMemoryBySystem) { + self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageSystemCache; + + } else { + self.carImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; - [self.carImageView reloadAnimationImages]; - [self.carImageView setInteractiveAnimation:YES]; - break; - - default: - break; - } + } + + if (self.demoExemple & JMDemoReverseImage) { + self.carImageView.imageOrder = JMAnimatedImageViewOrderReverse; + } + + self.imageView.hidden = YES; + self.carImageView.hidden = NO; + self.carImageView.animationDelegate = self; + self.carImageView.animationDatasource = self; + self.carImageView.animationRepeatCount = 0; + self.carImageView.animationDuration = 4.0; + + if (self.demoExemple & JMDemoAutomatic) { + [self.carImageView startAnimating]; + } else { + [self.carImageView setCurrentIndex:0 animated:NO]; + } } #pragma mark - JMOImageViewAnimationDatasource - (NSInteger)numberOfImagesForAnimatedImageView:(UIImageView *)imageView { - if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) { + if (self.demoExemple & JMDemoPhotos) { return 11; } return 70; @@ -134,7 +80,7 @@ - (NSInteger)numberOfImagesForAnimatedImageView:(UIImageView *)imageView - (NSString *)imageNameAtIndex:(NSInteger)index forAnimatedImageView:(UIImageView *)imageView { - if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) { + if (self.demoExemple & JMDemoPhotos) { return [NSString stringWithFormat:@"%d_verge_super_wide.jpg",(int)index]; } @@ -143,7 +89,7 @@ - (NSString *)imageNameAtIndex:(NSInteger)index forAnimatedImageView:(UIImageVie - (NSInteger)firstIndexForAnimatedImageView:(UIImageView *)imageView { - if (self.animationType == JMAnimatedImageViewAnimationTypeManualSwipe) { + if (self.demoExemple & JMDemoPhotos) { return 0; } diff --git a/README.md b/README.md index 7b00141..6884707 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,16 @@ JMAnimatedImageView is a performant subclass of UIImageView: - Can use has a Carousel, - GIF are supported to load your animations. -## Installation & Usage +## Change Log + +0.2.4 : + +- Improve documentation +- Fix retain cycle (Thanks Instruments!) +- JMGif allocation for a better integration with your favorites network libraries. + + +## Installation Simply replace your `UIImageView` instances with instances of `JMAnimatedImageView`. @@ -18,9 +27,12 @@ If using CocoaPods, the quickest way to try it out is to type this on the comman $ pod try JMAnimatedImageView ``` +## Usage +### For a local animation using file from a bundle + In your code, `#import "JMAnimatedImageView.h"` and `#import "JMAnimatedImageView.h"` -```objective-c +``` //GIF example @property (weak, nonatomic) IBOutlet JMAnimatedImageView *jmImageView; @@ -29,7 +41,7 @@ self.jmImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinear [self.jmImageView startAnimating]; ``` -```objective-c +``` //PNG example with manual animation @property (weak, nonatomic) IBOutlet JMAnimatedImageView *jmImageView; @@ -38,13 +50,46 @@ self.jmImageView.animationDatasource = self; [self.jmImageView reloadAnimationImages]; // self.jmImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; self.jmImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; +[self.jmImageView startAnimating]; +``` + +### For a remote Gif + +```objective-c +@property (weak, nonatomic) IBOutlet JMAnimatedImageView *jmImageView; + +[[JMApi sharedApi] downloadYourGifFileHasData:^(NSData *gifData) { + self.animatedImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; + self.animatedImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; + [self.animatedImageView reloadAnimationImagesFromGifData:gifData fromUrl:url]; + [self.animatedImageView startAnimating]; +}]; ``` +### For a remote Gif using AFNetworking +```objective-c + NSURLRequest *req = [NSURLRequest requestWithURL:url]; + + AFHTTPRequestOperation *postOperation = [[AFHTTPRequestOperation alloc] initWithRequest:req]; + [postOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { + self.animatedImageView.animationType = JMAnimatedImageViewAnimationTypeAutomaticLinearWithoutTransition; + self.animatedImageView.memoryManagementOption = JMAnimatedImageViewMemoryLoadImageLowMemoryUsage; + [self.animatedImageView reloadAnimationImagesFromGifData:responseObject fromUrl:url]; + [self.animatedImageView startAnimating]; + + } failure:^(AFHTTPRequestOperation *operation, NSError *error) { + NSLog(@"Image error: %@", error); + block(NO, nil); + }]; + + [postOperation start]; +``` -Some parameters : +### Customizations * AnimationType -```objc + +```objective-c typedef NS_ENUM(NSUInteger, JMAnimatedImageViewAnimationType) { JMAnimatedImageViewAnimationTypeInteractive = 0, @@ -59,22 +104,18 @@ typedef NS_ENUM(NSUInteger, JMAnimatedImageViewAnimationType) { ``` * MemoryOption -```objc + +```objective-c typedef NS_ENUM(NSUInteger, JMAnimatedImageViewMemoryOption) { - //images memory will be retain by system - JMAnimatedImageViewMemoryLoadImageSystemCache = 0, - - //image are loaded in live - JMAnimatedImageViewMemoryLoadImageLowMemoryUsage, - - //you load your images has you want - JMAnimatedImageViewMemoryLoadImageCustom + JMAnimatedImageViewMemoryLoadImageSystemCache = 0, //images memory will be retain by system + JMAnimatedImageViewMemoryLoadImageLowMemoryUsage, //images loaded but not retained by the system + JMAnimatedImageViewMemoryLoadImageCustom //images loaded by you (JMOImageViewAnimationDatasource) }; ``` * ImageViewOrder -```objc +```objective-c typedef NS_ENUM(NSUInteger, JMAnimatedImageViewOrder) { JMAnimatedImageViewOrderNormal = 1, JMAnimatedImageViewOrderReverse = -1