封装显示不同类型图片

//MARK: - 动态设置入口图片

-(void)setEntranceIcon{
    NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://xxxxxx"]];
       YYImageType type = YYImageDetectType((__bridge CFDataRef)data);
       if (type == YYImageTypeGIF){
            [self.sysBtn setImage: [UIImage animatedGIFWithData:data] forState:UIControlStateNormal];
       }else{
           [self.sysBtn setImage:[UIImage imageWithData:data] forState:UIControlStateNormal];
       }
}

+ (UIImage *)animatedGIFWithData:(NSData *)data
{
    if (!data) {
        return nil;
    }
    
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    
    size_t count = CGImageSourceGetCount(source);
    
    UIImage *animatedImage;
    
    if (count <= 1) {
        animatedImage = [[UIImage alloc] initWithData:data];
    }
    else {
        NSMutableArray *images = [NSMutableArray array];
        
        NSTimeInterval duration = 0.0f;
        
        for (size_t i = 0; i < count; i++) {
            CGImageRef image = CGImageSourceCreateImageAtIndex(source, i, NULL);
            duration += [self frameDurationAtIndex:i source:source];
            
            [images addObject:[UIImage imageWithCGImage:image
                                                  scale:[UIScreen mainScreen].scale
                                            orientation:UIImageOrientationUp]];
            CGImageRelease(image);
        }
        
        if (!duration) {
            duration = (1.0 / 10.0f) * count;
        }
        
        animatedImage = [UIImage animatedImageWithImages:images duration:duration];
    }
    
    CFRelease(source);
    
    return animatedImage;
}

你可能感兴趣的:(封装显示不同类型图片)