iOS中的圖片

1、將圖片保存到相冊(cè)
<pre>
UIImage *image = [UIImage imageNamed:@"1.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
</pre>

2、圖片格式轉(zhuǎn)化(JPG和PNG)
<pre>UIImagePNGRepresentation(image); //轉(zhuǎn)化為png
UIImageJPEGRepresentation(image, 1); //轉(zhuǎn)化為jpeg
最后1個(gè)參數(shù):0-1,0壓縮最大,質(zhì)量差,1壓縮最小,質(zhì)量最好</pre>

3、將GIF圖片分解為單幀,轉(zhuǎn)化為UIImage保存到數(shù)組中

<pre>#import <ImageIO/ImageIO.h>

import <MobileCoreServices/MobileCoreServices.h><br />

NSString *gifPath = [[NSBundle mainBundle] pathForResource:@"123.gif" ofType:nil];
NSData *data = [NSData dataWithContentsOfFile:gifPath];
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)data, NULL); // 獲取數(shù)據(jù)源
size_t count = CGImageSourceGetCount(source);// 獲取幀數(shù)
NSMutableArray *tempArray = [NSMutableArray array];
for (size_t i = 0; i < count; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(source, i, NULL); // 獲取單幀圖片
UIImage *image = [UIImage imageWithCGImage:imageRef scale:[UIScreen mainScreen].scale orientation:UIImageOrientationUp];
[tempArray addObject:image];
CGImageRelease(imageRef); // 釋放單幀圖片
}
CFRelease(source); // 釋放數(shù)據(jù)源
</pre>

4、展示幀動(dòng)畫(huà)
<pre>
NSMutableArray *tempArray = [NSMutableArray array];
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, 200)];
[self.view addSubview:imageView];
NSInteger pictureCount = 8;
for (int i = 0; i < pictureCount; i++) {
NSString *imageString = [NSString stringWithFormat:@"IMG_%02d.jpg",i];
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageString ofType:nil];
// 注意此處不要使用[UIImage imageNamed:imageString]方法
// 使用上面的方法后會(huì)有內(nèi)存緩存,內(nèi)存飆升,容易導(dǎo)致程序閃退,優(yōu)點(diǎn)是再次加載的時(shí)候直接從內(nèi)存中取,速度很快
// imageWithContentsOfFile,不會(huì)有緩存
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[tempArray addObject:image];
}

imageView.animationImages = tempArray; // 設(shè)置幀動(dòng)畫(huà)數(shù)組
imageView.animationRepeatCount = 1; // 設(shè)置動(dòng)畫(huà)重復(fù)次數(shù)
imageView.animationDuration = pictureCount * 0.1; // 設(shè)置動(dòng)畫(huà)持續(xù)時(shí)間

[imageView startAnimating];

// 在動(dòng)畫(huà)播放完成后清理內(nèi)存
[imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:5];
</pre>

5、用單幀圖片合成gif圖片
<pre>
// 1.獲取圖片數(shù)組
NSMutableArray *imageArray = [NSMutableArray array];
NSInteger pictureCount = 8;
for (int i=0; i < pictureCount; i++) {
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d.png",i] ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:path];
[imageArray addObject:image];
}
// 2.獲取文件地址
NSString *docPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folder = [docPath stringByAppendingPathComponent:@"gif"];
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:NULL];
NSString *path = [folder stringByAppendingPathComponent:@"shark.gif"];
NSLog(@"path>>>>%@",path);

// 3.生成gif地址
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
CGImageDestinationRef destination = CGImageDestinationCreateWithURL(url, kUTTypeGIF, pictureCount, NULL);
for (UIImage *image in imageArray) {
CGImageDestinationAddImage(destination, image.CGImage, nil);
}
CGImageDestinationFinalize(destination);
CFRelease(destination);</pre>

6、關(guān)于藍(lán)色文件夾和黃色文件夾
黃色文件夾可以直接訪問(wèn),藍(lán)色的文件夾的訪問(wèn)方式需要加相關(guān)路徑
<pre>
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"picture/1.jpg" ofType:nil];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
或者
self.imageView.image = [UIImage imageNamed:@"picture/1.jpg"];
</pre>

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

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

  • 這是我的第一個(gè)項(xiàng)目,雖說(shuō)是接手別人的,但是也很激動(dòng)的說(shuō). 在項(xiàng)目中,需要改需求,要計(jì)算下載速度,其中用到了使用表盤(pán)...
    atiman閱讀 5,881評(píng)論 0 2
  • 媽媽說(shuō),記得以后當(dāng)了律師一定要感謝她。 初中,青春超級(jí)叛逆期。處于一個(gè)“我的青春我做主”的屌炸天的時(shí)期。那個(gè)時(shí)候,...
    一個(gè)好的壞人is閱讀 208評(píng)論 0 0
  • 把你從回憶里剝離,一絲一縷。這抽出來(lái)的絲是我對(duì)你的愛(ài)與思念,我在沒(méi)有你的日子里作繭自縛。這繭堅(jiān)硬而纏綿,我出不來(lái)...
    Mar墨殤閱讀 335評(píng)論 0 0
  • 估計(jì)大家都聽(tīng)過(guò)無(wú)數(shù)遍的一句話:“聽(tīng)過(guò)很多道理,依然過(guò)不好這一生?!?01 我也聽(tīng)過(guò)很多次,甚至還有室友問(wèn)我,你覺(jué)得...
    笑自來(lái)閱讀 804評(píng)論 6 4
  • 本文寫(xiě)給那些想極速集成推送功能的開(kāi)發(fā)者,不用閱讀任何文檔,不用寫(xiě)一行代碼,1分鐘集成,立即使用! 不論你的項(xiàng)目是用...
    pikacode閱讀 2,792評(píng)論 2 22

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