首页 > 代码库 > iOS WebP图片格式
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图片格式
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。