首页 > 代码库 > 图片操作,CoreImage、存储、截屏

图片操作,CoreImage、存储、截屏

1.图片的处理,CoreImage

添加CoreImage的属性,并生成synthesize

@property (nonatomic,strong) CIContext *context;
@property (nonatomic,strong) CIFilter *filter1;
@property (nonatomic,strong) CIFilter *filter2;
@property (nonatomic,strong) CIFilter *filter3;
@property (nonatomic,strong) CIImage *beginImage;
@synthesize context;
@synthesize filter1;
@synthesize filter2;
@synthesize filter3;
@synthesize beginImage;

查看所有的过滤器信息:

- (void)logAllFilters {
    NSArray *properties = [CIFilter filterNamesInCategory:kCICategoryBuiltIn];
    NSLog(@"count=%d\n,filterName=%@\n",properties.count,properties);
    
    for (NSString *str in properties) {
        CIFilter *filter = [CIFilter filterWithName:str];
        NSLog(@"%@:\n%@",str,[filter attributes]);
    }
}

创建CIImage对象:

#if 1
    //通过图片路径创建CIImage对象
    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"jpg"];
    NSURL *fileNameAndPath = [NSURL fileURLWithPath:filePath];
    beginImage = [CIImage imageWithContentsOfURL:fileNameAndPath];
#else
    /*通过UIImage创建*/
    UIImage *image = [UIImage imageNamed:@"1.jpg"];
    beginImage = [CIImage imageWithCGImage:image.CGImage];
#endif

创建CIContext:

#if 1
    //创建基于GPU的CIContext对象,效率高,但不支持跨应用访问,若在imagePicker里操作会使GPU降为CPU
    context = [CIContext contextWithOptions:nil];
#else
    //创建基于CPU的CIContext对象
    context = [CIContext contextWithOptions:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:kCIContextUseSoftwareRenderer]];
#endif

设置过滤器:

    filter1 = [CIFilter filterWithName:@"CISepiaTone"];
    filter2 = [CIFilter filterWithName:@"CIHueAdjust"];
    filter3 = [CIFilter filterWithName:@"CIStraightenFilter"];

增加slider,在valueChanged时添加事件:

    /*设置过滤器参数*/
    //指定需要处理的图片
    [filter1 setValue:beginImage forKey:kCIInputImageKey];
    //指定过滤的参数(filter2和filter3的key为inputAngle
[filter1 setValue:[NSNumber numberWithFloat:slider1.value] forKey:@"inputIntensity"];
//得到过滤后的图片
CIImage *outputImage = [filter1 outputImage];
//转换图片
CGImageRef imgRef = [context createCGImage:outputImage fromRect:[outputImage extent]];
UIImage *newImage = [UIImage imageWithCGImage:imgRef];
//显示图片
imgView.image = newImage;
//释放c对象
CGImageRelease(imgRef);

2.图片保存

存到手机图库:

#import <AssetsLibrary/AssetsLibrary.h>

    CIImage *saveToSave = [filter1 outputImage];
    CGImageRef imgRef = [context createCGImage:saveToSave fromRect:[saveToSave extent]];
  ALAssetsLibrary *library = [[ALAssetsLibraryalloc] init];
  
[library writeImageToSavedPhotosAlbum:imgRef metadata:[saveToSave properties] completionBlock:^(NSURL *assetURL,NSError *error){
    CGImageRelease(imgRef);
 }];

以图片的形式存到沙盒目录下:

- (void)saveImage:(UIImage *)image withName:(NSString *)nameStr {

    NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:nameStr];

    [UIImagePNGRepresentation(image) writeToFile:path atomically:NO];

    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];

    NSLog(@"%@\n存储到docments目录下的文件有:%@",NSHomeDirectory(),[[NSFileManagerdefaultManager] subpathsAtPath:docPath]);

}

以网络缓存的形式:(查找时先将url地址进行MD5加密,按加密后的名称查找)

- (void)saveImage:(UIImage *)image withURLString:(NSString *)urlString {
    NSString *path = [NSString stringWithFormat:@"%@/tmp",NSHomeDirectory()];
    NSLog(@"%@",path);
    [UIImagePNGRepresentation(image) writeToFile:[NSString stringWithFormat:@"%@/%@",path,[urlString MD5Hash]] atomically:NO];
    NSLog(@"存储到tmp目录下的文件有:%@",[[NSFileManager defaultManager] subpathsAtPath:path]);
}

3.实现截屏

UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    ImageViewController *ivc = [[ImageViewController alloc] init];
    [self.navigationController pushViewController:ivc animated:YES];
    ivc.image = image;

在第二个页面添加属性image