iOS圖片處理

來源:互聯網
上載者:User

標籤:

      我們常用的圖片格式可以分為bmp,jpg,png,gif,webp,其中bmp常用語安卓端開發,iOS常用的是jpg和png,蘋果預設是不支援gif圖片的,我們拉取gif得到的是一幀幀的圖片但是我們可以在工程中用代碼產生gif圖片,webp是google推出的一種新的圖片格式,它的有點是可以將相同品質的圖片大小縮減50%甚至更多,webp演算法比較複雜,消耗記憶體較多,但基於其儲存佔用優勢,以後可能會成為主流格式。

   下邊先從jpg和png談起,iOS中我們常用png,因為清晰度相同的兩張圖片,png是無損的,所佔空間更小。

  一、png和jpg相互轉化

//jpg轉化png-(void)jpgToPng{    UIImage * image = [UIImage imageNamed:@"1.jpg"];        NSData * data = UIImagePNGRepresentation(image);        UIImage * pngImage =[UIImage imageWithData:data];        UIImageWriteToSavedPhotosAlbum(pngImage, nil, nil, nil);    }//jpg轉化jpg-(void)jpgTojpg{    UIImage * image = [UIImage imageNamed:@"1.jpg"];    //後一個參數越小,則產生的圖片越小,越模糊    NSData * data = UIImageJPEGRepresentation(image, 0.5);        UIImage * jpgImage =[UIImage imageWithData:data];        UIImageWriteToSavedPhotosAlbum(jpgImage, nil, nil, nil);    }//png轉化png-(void)pngToJpg{    UIImage * image = [UIImage imageNamed:@"2.png"];        NSData * data = UIImageJPEGRepresentation(image, 0.5);        UIImage * jpgImage =[UIImage imageWithData:data];        UIImageWriteToSavedPhotosAlbum(jpgImage, nil, nil, nil);    }

二、gif圖片分解

gif圖片分解的步驟為

1.拿到gif資料

2.分幀

3.將單幀資料轉化為圖片

4.儲存

首先我們需要先引入標頭檔


#import <ImageIO/ImageIO.h>

#import <MobileCoreServices/MobileCoreServices.h>

//分解gif-(void)deCompositionGif{ //讀取gif資料    NSString * gifResource = [[NSBundle mainBundle]pathForResource:@"1.gif" ofType:nil];            NSData * data = [NSData dataWithContentsOfFile:gifResource];       CGImageSourceRef  source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL);       //擷取gif幀數    size_t count = CGImageSourceGetCount(source);    NSLog(@"%ld",count);    //定義數組,儲存單幀圖片        NSMutableArray * ImageArr = [NSMutableArray array];    //遍曆取出每一幀    for (size_t i = 0; i<count; i++) {               CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL);                        //將單幀資料轉化為uiimag   scale 圖片物理尺寸與螢幕的比例    orientation 圖片的方向        UIImage * image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];        //儲存        [ImageArr addObject:image];        //釋放        CGImageRelease(imageRef);    }    CFRelease(source);    //單幀圖片儲存    int i  = 0 ;    for (UIImage * image in ImageArr) {        i++;        NSData *  data = UIImagePNGRepresentation(image);                NSArray * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        NSString * gifPath = path[0];                NSString * pathNumber = [gifPath stringByAppendingString:[NSString stringWithFormat:@"%d.png",i]];        NSLog(@"%@",pathNumber);        [data writeToFile:pathNumber atomically:YES];    }    }

三.gif展示

 UIImageView * imageView3 =[[UIImageView alloc]initWithFrame:CGRectMake(10, 100, 200, 200)];    [self.view addSubview:imageView3];    NSMutableArray * arr = [[NSMutableArray alloc]init];        for (int i = 1; i<9; i++) {        UIImage * image = [UIImage imageNamed:[NSString stringWithFormat:@"Documents%d",i]];                [arr addObject:image];    }    //設定播放屬性    [imageView3 setAnimationImages:arr];    [imageView3 setAnimationDuration:2];    [imageView3 setAnimationRepeatCount:10];    [imageView3 startAnimating];

四.由幀圖片產生gif。

//建立gif圖片-(void)creatGif{    //擷取gif資料    NSMutableArray * image =[[NSMutableArray alloc]initWithObjects:[UIImage imageNamed:@"Documents1"],[UIImage imageNamed:@"Documents2"],[UIImage imageNamed:@"Documents3"],[UIImage imageNamed:@"Documents4"],[UIImage imageNamed:@"Documents5"],[UIImage imageNamed:@"Documents6"], nil];//建立gif檔案    NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);        NSString * documentStr = [document objectAtIndex:0];        NSFileManager * manager = [NSFileManager defaultManager];    NSString * testDic = [documentStr stringByAppendingString:@"/gif"];    NSString * path = [testDic stringByAppendingString:@"test.gif"];    [manager createDirectoryAtPath:testDic withIntermediateDirectories:YES attributes:nil error:nil];//配置屬性    CGImageDestinationRef destion;    //將path映射成 CFURLRef 對象    CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);       //url 路徑 kUTTypeGIF目標類型  destion  =    CGImageDestinationCreateWithURL(url, kUTTypeGIF, image.count, nil);        NSDictionary * frameDic = [NSDictionary dictionaryWithObject:[NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3], (NSString *)kCGImagePropertyGIFDelayTime,nil] forKey:(NSString *)kCGImagePropertyGIFDelayTime];        NSMutableDictionary * gifParmDic = [NSMutableDictionary dictionaryWithCapacity:3];        [gifParmDic setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];        [gifParmDic setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];    [gifParmDic setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];    [gifParmDic setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];    NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParmDic forKey:(NSString *)kCGImagePropertyGIFDictionary ];    //單幀圖片添加到gif    for (UIImage * Dimage  in  image) {        CGImageDestinationAddImage(destion,Dimage.CGImage, (__bridge CFDictionaryRef)frameDic);                   }    CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);        CGImageDestinationFinalize(destion);        CFRelease(destion);}

 

iOS圖片處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.