iOSgif图片播放

#import 
#import 

@interface NSArray (HJImages)

+ (NSArray *)hj_imagesWithLocalGif:(NSString *)gifName;

+ (NSArray *)hj_imagesWithLocalGif:(NSString *)gifName expectSize:(CGSize)size;
#import "NSArray+HJImages.h"
#import 


@implementation NSArray (HJImages)


+ (NSArray *)hj_imagesWithLocalGif:(NSString *)gifName;{
    
    return [self hj_imagesWithLocalGif:gifName expectSize:CGSizeZero];
}


+ (NSArray *)hj_imagesWithLocalGif:(NSString *)gifName expectSize:(CGSize)size{
    
    BOOL needResize = (size.height!=0 && size.width!=0);
    
    NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:gifName withExtension:@"gif"];
    CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
    size_t gifCount = CGImageSourceGetCount(gifSource);
    NSMutableArray *frames = [[NSMutableArray alloc]init];
    for (size_t i = 0; i< gifCount; i++) {
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
        UIImage *image = [UIImage imageWithCGImage:imageRef];
        if (needResize) {
            image = [self imageByScalingAndCroppingForSize:size withImage:image];
        }
        [frames addObject:image];
        CGImageRelease(imageRef);
    }
    return frames;
    
}

//图片压缩到指定大小
+ (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize withImage:(UIImage *)sourceImage
{
    UIImage *newImage = nil;
    CGSize imageSize = sourceImage.size;
    CGFloat width = imageSize.width;
    CGFloat height = imageSize.height;
    CGFloat targetWidth = targetSize.width;
    CGFloat targetHeight = targetSize.height;
    CGFloat scaleFactor = 0.0;
    CGFloat scaledWidth = targetWidth;
    CGFloat scaledHeight = targetHeight;
    CGPoint thumbnailPoint = CGPointMake(0.0,0.0);
    
    if (CGSizeEqualToSize(imageSize, targetSize) == NO)
    {
        CGFloat widthFactor = targetWidth / width;
        CGFloat heightFactor = targetHeight / height;
        
        if (widthFactor > heightFactor)
            scaleFactor = widthFactor; // scale to fit height
        else
            scaleFactor = heightFactor; // scale to fit width
        scaledWidth= width * scaleFactor;
        scaledHeight = height * scaleFactor;
        
        // center the image
        if (widthFactor > heightFactor)
        {
            thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5;
        }
        else if (widthFactor < heightFactor)
        {
            thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5;
        }
    }
    
    UIGraphicsBeginImageContext(targetSize); // this will crop
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = thumbnailPoint;
    thumbnailRect.size.width= scaledWidth;
    thumbnailRect.size.height = scaledHeight;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    if(newImage == nil)
        NSLog(@"could not scale image");
    
    //pop the context to get back to the default
    UIGraphicsEndImageContext();
    return newImage;
}


使用方法:

    /** 设置gif图片播放器 */
    self.bgImageView.animationImages = [NSArray hj_imagesWithLocalGif:@"gif图片名字" expectSize:self.view.bounds.size];
    [self.bgImageView startAnimating];

你可能感兴趣的:(iOSgif图片播放)