iOS中的图片
1、将图片保存到相册
<pre>
UIImage *image = [UIImage imageNamed:@"1.jpg"];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
</pre>
2、图片格式转化(JPG和PNG)
<pre>UIImagePNGRepresentation(image); //转化为png
UIImageJPEGRepresentation(image, 1); //转化为jpeg
最后1个参数:0-1,0压缩最大,质量差,1压缩最小,质量最好</pre>
3、将GIF图片分解为单帧,转化为UIImage保存到数组中
<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); // 获取数据源
size_t count = CGImageSourceGetCount(source);// 获取帧数
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); // 释放数据源
</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]方法
// 使用上面的方法后会有内存缓存,内存飙升,容易导致程序闪退,优点是再次加载的时候直接从内存中取,速度很快
// imageWithContentsOfFile,不会有缓存
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
[tempArray addObject:image];
}
imageView.animationImages = tempArray; // 设置帧动画数组
imageView.animationRepeatCount = 1; // 设置动画重复次数
imageView.animationDuration = pictureCount * 0.1; // 设置动画持续时间
[imageView startAnimating];
// 在动画播放完成后清理内存
[imageView performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:5];
</pre>
5、用单帧图片合成gif图片
<pre>
// 1.获取图片数组
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、关于蓝色文件夹和黄色文件夹
黄色文件夹可以直接访问,蓝色的文件夹的访问方式需要加相关路径
<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>