码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-07-22 23:14:15      阅读:369      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   

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;

查看所有的过滤器信息:

mamicode.com,码迷
- (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]);
    }
}
mamicode.com,码迷

创建CIImage对象:

mamicode.com,码迷
#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
mamicode.com,码迷

创建CIContext:

mamicode.com,码迷
#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
mamicode.com,码迷

设置过滤器:

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

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

mamicode.com,码迷
    /*设置过滤器参数*/
    //指定需要处理的图片
    [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);
mamicode.com,码迷

2.图片保存

存到手机图库:

#import <AssetsLibrary/AssetsLibrary.h>

mamicode.com,码迷
    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);
 }];
mamicode.com,码迷

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

mamicode.com,码迷

- (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]);

}

mamicode.com,码迷

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

mamicode.com,码迷
- (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]);
}
mamicode.com,码迷

3.实现截屏

mamicode.com,码迷
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;
mamicode.com,码迷

在第二个页面添加属性image

图片操作,CoreImage、存储、截屏,码迷,mamicode.com

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

标签:style   blog   http   java   color   os   

原文地址:http://www.cnblogs.com/xiaochaozi/p/3699921.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!