iOS WebP圖片格式

來源:互聯網
上載者:User

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) {                    }];    }        }

效果:

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.