1、將圖片保存到相冊
<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個參數(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、展示幀動畫
<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]方法
// 使用上面的方法后會有內(nèi)存緩存,內(nèi)存飆升,容易導致程序閃退,優(yōu)點是再次加載的時候直接從內(nèi)存中取,速度很快
// imageWithContentsOfFile,不會有緩存
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[tempArray addObject:image];
}
imageView.animationImages = tempArray; // 設(shè)置幀動畫數(shù)組
imageView.animationRepeatCount = 1; // 設(shè)置動畫重復次數(shù)
imageView.animationDuration = pictureCount * 0.1; // 設(shè)置動畫持續(xù)時間
[imageView startAnimating];
// 在動畫播放完成后清理內(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)于藍色文件夾和黃色文件夾
黃色文件夾可以直接訪問,藍色的文件夾的訪問方式需要加相關(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>