你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS WebP圖片格式

iOS WebP圖片格式

編輯:IOS開發綜合

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

效果:

 

\

 

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved