图片操作,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

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

时间: 2025-01-02 13:21:24

图片操作,CoreImage、存储、截屏的相关文章

iOS开发-检测用户截屏, 并获取所截图片

微信可以检测到用户截屏行为(Home + Power),并在稍后点击附加功能按钮时询问用户是否要发送刚才截屏的图片,这个用户体验非常好.于是乎, 我也想着实现这个功能. 在iOS7之前, 如果用户截屏,系统会自动取消屏幕上的所有 touch 事件,(使用 touchesCancelled:withEvent: 这个方法)那么我们就可以检测这个方法的调用,然后加载本地最新图片再加以判断来实现我们的目的.但在 iOS 7 之后,截屏不再会取消屏幕的 touch 事件,所以导致了 Snapchat 和

系统截屏源码浅析

android中实现截屏的方式有很多种,形如下面几种: 1.通过view.getDrawingCache获取屏幕的图像数据,这也是众多开发同行朋友经常使用的一种方式,可惜的是这种方式并不适用于surfaceview. 2.利用adb命令,adb shell screencap -p path,再利用runtime去执行,但是这种方式需要获得系统权限方可. 3.通过framebuffer实现截屏,帧缓冲(framebuffer)是Linux为显示设备提供的一个接口,允许上层应用程序在图形模式下直接

各手机截屏方法收集

很多时候需要把手机截屏保存下来,现收集各种方法以供使用. 一.把手机连接电脑,用豌豆荚.91助手.360手机助手等手机助手软件进行截图,这种方式的好处是,可以给手机的外型也“截个图”. 二.针对各手机自带的截屏功能如下: 1.三星手机截屏 1)同时按住手机中间的home键+电源键,等手机咔嚓一声,整个屏幕就会被截取下来.注意一定是同时按住,否则很有可能截取不下来. 2)在图册中有个screenshots相册用来存放截屏下来的图片 2.酷派手机截屏 1)安卓版本4.0以上 在手机上同时按住开机键和

robotFramework——截屏

测试执行过程中进行截屏并且保存,是任何一款自动化测试工具或者框架必备的功能.那么Robotframework如何进行截屏呢?Robotframework提供了一个“Screenshot”库.    使用“Screenshot”库前,需要确认window环境中是否已经安装以下软件:        wxPython (地址 http://wxpython.org)               Python Imaging Library (PIL) (地址 http://www.pythonware

nodejs+phantomjs+七牛 实现截屏操作并上传七牛存储

近来研究了下phantomjs,只是初涉,还谈不上深入研究,首先介绍下什么是phantomjs. 官网上的介绍是:”PhantomJS is a headless WebKit scriptable with a JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.”翻译过来就是:”PhantomJS

【Android实战】Bitmap图片的截屏、模糊处理、传递、使用

项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕.并跳转到另外一个页面,同一时候将这个截屏图片作为下一个页面的背景图片,同一时候背景图片须要模糊处理 接下来就一步一步解决这个问题: 1.截取无状态栏的当前屏幕图片.请參考takeScreenShot方法 2.使图片高斯模糊的方法请參考blurBitmap方法 注意:RenderScript是Android在API 11之后增加的,用于高效的图片处理,包含模糊.混合.矩阵卷积计算等 public class ScreenShotUtil

Android之Bitmap图片的截屏、模糊处理、传递、使用

项目中遇到了这样一个需求: 当某个条件满足时就截取当前屏幕,并跳转到另外一个页面,同时将这个截屏图片作为下一个页面的背景图片,同时背景图片需要模糊处理 接下来就一步一步解决问题: 1.截取无状态栏的当前屏幕图片,请参考takeScreenShot方法 2.使图片高斯模糊的方法请参考blurBitmap方法 注意:RenderScript是Android在API 11之后加入的,用于高效的图片处理,包括模糊.混合.矩阵卷积计算等 public class ScreenShotUtil { // 获

selenium webdriver 截屏操作

有时候我们需要进行截屏操作,特别是遇到一些比较重要的页面信息(出现错误)或者出现不同需要进行对比时, 我们就需要对正在处理的页面进行截屏! 未经作者允许,禁止转载! package test_wait20161205; import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.openqa.selenium.OutputType; import org.o

完美解决scrollView 截屏图片模糊

UIGraphicsBeginImageContext   首先说明一下UIGraphicsBeginImageContextWithOptions 和UIGraphicsBeginImageContext区别:   1: UIGraphicsBeginImageContext(CGSize size) 参数size为新创建的位图上下文的大小.它同时是由UIGraphicsGetImageFromCurrentImageContext函数返回的图形大小. 该函数的功能同UIGraphicsBeg