[UIImage系列]-圖片jpg和png格式的轉(zhuǎn)化以及gif圖的分解和合成

忙里偷閑寫篇小文。

本文講述關(guān)于iOS中處理圖片的相關(guān)問題。
通常情況下,我們處理的圖片多為png或者jpg格式的,所以在此我們先看看關(guān)于這兩種格式的圖片的顯示、獲取以及相互轉(zhuǎn)化。
首先導(dǎo)入頭文件

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
UIImage *image = [UIImage imageNamed:@"myopic.jpg"];
    
// 將圖片保存到手機相冊
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

上述便是將名為mypic.jpg的圖片保存至手機相冊的基本操作。

接下來看看如果有一個jpg格式的圖片,我們怎么將其轉(zhuǎn)化為png的格式:

UIImage *image = [UIImage imageNamed:@"pic.jpg"];
NSData *data = UIImagePNGRepresentation(image);
UIImage *image_png = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(image_png, nil, nil, nil);

其實可以看出我們基本的思路可以分為:
1.獲取需要處理的圖片
2.將圖片轉(zhuǎn)化為NSData類型的數(shù)據(jù)
3.通過“imageWithData”的方式將圖片讀取
在轉(zhuǎn)化為NSData類型的時候就是我們?nèi)ミx擇轉(zhuǎn)為png或者jpg格式的時候、具體為:
UIImagePNGRepresentation (image)和UIImageJPEGRepresentation(image, scale)

以下為png格式和jpg格式轉(zhuǎn)化為jpg格式的處理方式:

    UIImage *image = [UIImage imageNamed:@"pic1.png"];
    // 圖片的質(zhì)量因子
    // 質(zhì)量因子越小、生成的jpg圖片size越小、清晰度越低
    NSData *data = UIImageJPEGRepresentation(image, 1);
    UIImage *image_jpg = [UIImage imageWithData:data];
    UIImageWriteToSavedPhotosAlbum(image_jpg, nil, nil, nil);
    
    
    UIImage *image2 = [UIImage imageNamed:@"pic1.jpg"];
    // 圖片的尺寸
    NSData *data2 = UIImageJPEGRepresentation(image2, 0.5);
    UIImage *image_jpg2 = [UIImage imageWithData:data2];
    UIImageWriteToSavedPhotosAlbum(image_jpg2, nil, nil, nil);

由此也可看出、png格式的圖片質(zhì)量是無損的,并且透明,而jpg格式的圖片因為我們可以去設(shè)置他的質(zhì)量因子,故而會造成圖片質(zhì)量的消損。

然后我們再看看關(guān)于gif格式圖片的處理。

iOS原生是不支持gif格式的圖片的,但是因為gif的圖片其實可以看作是一張張圖片通過動畫楨組合而成,故而我們可以通過這種思路進(jìn)行相應(yīng)的處理。
主要步驟可以分為以下四步:

  1. 獲取需要處理的gif圖片
  2. 將gif圖片分解為單幀
  3. 將單幀數(shù)據(jù)轉(zhuǎn)為UIImage
  4. 保存單幀圖片

具體的代碼如下示:

    // 獲取需要處理的gif圖片文件
    NSString *gifPathsource = [[NSBundle mainBundle] pathForResource:@"pic3" ofType:@"gif"];
    // 將拿到的gif文件轉(zhuǎn)化為NSData類型
    NSData *data = [NSData dataWithContentsOfFile:gifPathsource];
    // 將NSData類型數(shù)據(jù)轉(zhuǎn)為CGImageSourceRef類型
    CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);
    
    NSMutableArray *imageArray = [NSMutableArray array];
    // 分解為單幀圖片
    size_t count = CGImageSourceGetCount(source);
    for (size_t i=0; i < count; i++) {
        
        // 獲取到的單幀數(shù)據(jù)
        CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);
        // 將單幀數(shù)據(jù)轉(zhuǎn)化為UIImage數(shù)據(jù)
        UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
        [imageArray addObject:image];
        // 釋放緩存
        CGImageRelease(imageRef);
        
    }
    // 釋放緩存
    CFRelease(source);
    
    // 保存單幀圖片
    int i = 0;
    for (UIImage *image in imageArray) {
        
        NSData *data = UIImagePNGRepresentation(image);
        // 獲取本地文件夾的路徑
        NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        
        NSString *gifPath = path[i];
        
        NSString *pathNum = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];
        
        [data writeToFile:pathNum atomically:YES];
        
        i++;
    }

上面便是將gif圖片進(jìn)行分解、變成一幀楨的圖片(需要注意的是:我們要記得對獲取到的CGImageRef數(shù)據(jù)和CGImageSourceRef數(shù)據(jù)進(jìn)行釋放)。然后我們可以再將這些單幀圖片通過UIImageView來顯示在屏幕上。

    NSMutableArray *imageArray = [[NSMutableArray alloc] init];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 200, 150)];
    [self.view addSubview:imageView];
    
    for (int i = 0; i < 50; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"pic_%d.jpg",i]];
        [imageArray addObject:image];
    }
    
    // 設(shè)置動畫相關(guān)屬性
    [imageView setAnimationImages:imageArray];
    [imageView setAnimationRepeatCount:5];
    [imageView setAnimationDuration:2];
    // 開始執(zhí)行動畫
    [imageView startAnimating];

可以看出這里其實就是利用Animation的動畫方式進(jìn)行展示的。

下面是gif圖片的合成:

    // 1.獲取圖片數(shù)據(jù)
    NSMutableArray *imageArray = [NSMutableArray arrayWithObjects:[UIImage imageNamed:@"pic1.jpg"],[UIImage imageNamed:@"pic2.jpg"],[UIImage imageNamed:@"pic3.jpg"],[UIImage imageNamed:@"pic4.jpg"],[UIImage imageNamed:@"pic5.jpg"], nil];
    
    // 2.創(chuàng)建gif文件
    NSArray *document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docString = [document objectAtIndex:0];
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *pathString = [docString stringByAppendingString:@"/gif"];
    [fileManager createDirectoryAtPath:pathString withIntermediateDirectories:YES attributes:nil error:nil];
    NSString *path = [pathString stringByAppendingString:@"test1.gif"];
    NSLog(@"path:%@",path);
    
    // 3.配置gif屬性
    CGImageDestinationRef destion;
    // 將 path 映射成 CFURLRef 的路徑
    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
    destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imageArray.count, NULL);
    
    NSDictionary *dict = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil] forKey:(NSString *)kCGImagePropertyGIFDelayTime];
    
    NSMutableDictionary *mutDict = [NSMutableDictionary dictionaryWithCapacity:2];
    [mutDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
    [mutDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
    [mutDict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
    [mutDict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    NSDictionary *gifPropertyDict = [NSDictionary dictionaryWithObject:mutDict forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    // 單幀添加到gif
    for (UIImage *image in imageArray) {
        
        CGImageDestinationAddImage(destion, image.CGImage, (__bridge CFDictionaryRef)dict);
        
    }
    CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifPropertyDict);
    CGImageDestinationFinalize(destion);
    CFRelease(destion);

以上就是對iOS中圖片處理的一些總結(jié)。希望可以對大家有所幫助。

我是姣爺、我在簡書、加油!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 最近準(zhǔn)備給團隊傳授圖片格式的知識,于是開了谷歌,欲找些 PPT 素材,卻發(fā)現(xiàn)似乎沒有人好好寫過與圖片相關(guān)的知識,要...
    BenzLeung閱讀 12,483評論 1 37
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,062評論 25 709
  • 版本記錄 前言 只要是做圖片的或者與圖片相關(guān)的,那么圖片的格式就是一個不可以繞過的問題,我們見過很多的圖片格式,但...
    刀客傳奇閱讀 5,264評論 0 7
  • 今天跟著吳姐做垃圾袋,我跟吳姐折的方法不同,折了兩個,吳姐問我,你覺得我們兩的折疊的垃圾袋會有什么不一樣?。课叶⒘?..
    周溜遛閱讀 424評論 1 0
  • 今天功能機繼續(xù)帶給我種種生理不適,發(fā)短信時出現(xiàn)瑟瑟發(fā)抖的癥狀。書到用時方恨少,手機太小想出家。好在我迅速調(diào)整心態(tài),...
    我是一頭蒜閱讀 368評論 0 1

友情鏈接更多精彩內(nèi)容