码迷,mamicode.com
首页 > 移动开发 > 详细

iOS WebP图片格式

时间:2015-01-24 15:56:59      阅读:315      评论:0      收藏:0      [点我收藏+]

标签:ios   webp   高清   

google出品 高清晰,体量小得图片格式,下面说在iOS下如何使用

添加头文件

#import "UIImage+WebP.h"

#import "ViewController.h"
#import "UIImage+WebP.h"


@interface ViewController ()

@property (nonatomic ,strong) UIImageView *webPImageView;
@property (nonatomic ,strong) UILabel *webPLabel1;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
     NSString *normalImg = [[NSBundle mainBundle] pathForResource:@"changmen1" ofType:@"png"];
    
    uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:normalImg error:nil] fileSize];
    
    
    UIImage *demoImage = [UIImage imageNamed:@"changmen1.png"];
    UIImageView *pngView = [[UIImageView alloc] initWithImage:demoImage];
    pngView.frame = CGRectMake(0, 0, demoImage.size.width, demoImage.size.height);
    
    [self.view addSubview:pngView];
    UILabel *pngLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(pngView.frame) + 5,300, 10)];
    pngLabel.text = [NSString stringWithFormat:@"%@ format file size: %.2f KB ",[[normalImg pathExtension] uppercaseString],(double)fileSize / 1024];
    [pngLabel setFont:[UIFont systemFontOfSize:12]];
    [self.view addSubview:pngLabel];
    
    
    _webPImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(pngView.frame) + 20,demoImage.size.width, demoImage.size.height)];
    [self.view addSubview:_webPImageView];
    
    _webPLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(_webPImageView.frame) + 5,300, 10)];
    
    [_webPLabel1 setFont:[UIFont systemFontOfSize:12]];
    [self.view addSubview:_webPLabel1];
    
    
    [UIImage imageToWebP:demoImage quality:75.0 alpha:1.0 preset:WEBP_PRESET_PHOTO completionBlock:^(NSData *result) {
        [self displayImageWithData:result];
        
    } failureBlock:^(NSError *error) {
        
    }];
    
    
    
}


- (void)displayImageWithData:(NSData *)webPData
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *webPPath = [paths[0] stringByAppendingPathComponent:@"image.webp"];
    if ([webPData writeToFile:webPPath atomically:YES]) {
        [UIImage imageWithWebP:webPPath completionBlock:^(UIImage *result) {
            uint64_t fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:webPPath error:nil] fileSize];
            
            _webPLabel1.text = [NSString stringWithFormat:@"%@ format file size: %.2f KB ",[[webPPath pathExtension] uppercaseString],(double)fileSize / 1024];
            [_webPImageView setImage:result];
        } failureBlock:^(NSError *error) {
            
        }];
    }
    
    
}

效果:

技术分享


原文地址:http://blog.csdn.net/qqmcy/article/details/43085197

代码下载:http://download.csdn.net/detail/qqmcy/8392601



iOS WebP图片格式

标签:ios   webp   高清   

原文地址:http://blog.csdn.net/qqmcy/article/details/43085197

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!