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
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。