忙里偷閑寫篇小文。
本文講述關(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)的處理。
主要步驟可以分為以下四步:
- 獲取需要處理的gif圖片
- 將gif圖片分解為單幀
- 將單幀數(shù)據(jù)轉(zhuǎn)為UIImage
- 保存單幀圖片
具體的代碼如下示:
// 獲取需要處理的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é)。希望可以對大家有所幫助。
我是姣爺、我在簡書、加油!