iPhone/iOS图片相关(读取、保存、绘制、其它相关)
http://blog.csdn.net/jerryvon/article/details/7526147
20:50:42
一.读取图片
- UIImage* image=[UIImage imageNamed:@"1.jpg"];
- NSURL *url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"];
- UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];
- //读取本地图片非resource
- NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
- UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
- UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];
4.从现有的context中获得图像
- //add ImageIO.framework and #import <ImageIO/ImageIO.h>
- CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
- CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
- CGContextRef ctx=UIGraphicsGetCurrentContext();
- CGContextSaveGState(ctx);
- //transformCTM的2种方式
- //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(.2, -0.2));
- //CGContextScaleCTM(ctx,1,-1);
- //注意坐标要反下,用ctx来作为图片源
- CGImageRef capture=CGBitmapContextCreateImage(ctx);
- CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
- CGContextDrawImage(ctx, CGRectMake(160, 230, 160, 230), img);
- CGImageRef capture2=CGBitmapContextCreateImage(ctx);
5.用Quartz的CGImageSourceRef来读取图片
- CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
- CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
二.保存图片
- //保存图片 2种获取路径都可以
- //NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
- //NSString*documentsDirectory=[paths objectAtIndex:0];
- //NSString*aPath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",@"test"]];
- NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
- NSData *imgData = UIImageJPEGRepresentation(imgFromUrl,0);
- [imgData writeToFile:aPath atomically:YES];
三.绘制图(draw|painting)
- UIImageView* imageView=[[UIImageView alloc]initWithImage:image];
- imageView.frame=CGRectMake(0, 0, 320, 480);
- [self addSubview:imageView];
- [imageView release];
2.[img drawAtPoint]系列方法
- [image4 drawAtPoint:CGPointMake(100, 0)];
3.CGContextDrawImage
- CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
4.CGLayer
- CGLayerRef cg=CGLayerCreateWithContext(ctx, CGSizeMake(320, 480), NULL);
- //需要将CGLayerContext来作为缓存context,这个是必须的
- CGContextRef layerContext=CGLayerGetContext(cg);
- CGContextDrawImage(layerContext, CGRectMake(160, 230, 160, 230), img);
- CGContextDrawLayerAtPoint(ctx, CGPointMake(0, 0), cg);
5.CALayer的contents
- UIImage* image=[UIImage imageNamed:@"1.jpg"];
- CALayer *ly=[CALayer layer];
- ly.frame=CGRectMake(0, 0, 320, 460);
- ly.contents=[image CGImage];
- [self.layer addSublayer:ly];
四.其它
animationImage 设置完毕以后要startAnimation.不会自动启动动画图片。
此外在读取大量动画图片的时候不太适合用这个方法,因为一下子那么多图片容易爆掉。可以用这个方法替代,具体我也没试,方法就是手动切换图片,并非直接使用系统方法而已。
- imgV=[[UIImageView alloc]initWithFrame:CGRectMake(40, 40, 128, 128)];
- [self.window addSubview:imgV];
- [self performSelectorInBackground:@selector(playAnim)withObject:nil];
- [imgV release];
- -(void)playAnim{
- for (int i=0;i<101;){
- usleep(100000);
- UIImage *image=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i+1 ] ofType:@"tiff"]];
- [self performSelectorOnMainThread:@selector(changeImage:) withObject:image waitUntilDone:YES];
- i++;
- }
- }
- -(void)changeImage:(UIImage*)image{
- imgV.image=image;
- }
相关帖子:http://www.cocoachina.com/bbs/read.php?tid=110154
问题描述主要是有一个很小的叉按钮,需要响应很大的点击区域,这个其实很简单,代码如下:
- UIImage *bg=[UIImage imageNamed:@"heizi1.jpg"];
- //图片大于点及区域,缩小下就行
- bg=[self scaleImage:bg ToSize:(CGSize){100,100}];
- UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
- //图片大于button,则会被拉伸,如果小于button则居中显示
- [button setImage:bg forState:UIControlStateNormal];
此外多说一句,这个icon图片如果要准备2套图,缩放毕竟消耗效率
缩放图片代码
- -(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{
- UIImage *i;
- // CGSize itemSize=CGSizeMake(30, 30);
- UIGraphicsBeginImageContext(itemSize);
- CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);
- [img drawInRect:imageRect];
- i=UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return i;
从view截图出来
- #import <QuartzCore/QuartzCore.h>
- -(UIImage *)getImageFromView:(UIView *)orgView{
- UIGraphicsBeginImageContext(orgView.bounds.size);
- [orgView.layer renderInContext:UIGraphicsGetCurrentContext()];
- UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
iPhone/iOS图片相关(读取、保存、绘制、其它相关)的更多相关文章
- python图片的读取保存
#coding:utf-8 from PIL import Image import matplotlib.pyplot as plt img=Image.open("F:\\Upan\\源 ...
- 大屏iPhone的适配 +iOS 图片尺寸要求
摘自:http://blog.ibireme.com/2014/09/16/adapted_to_iphone6/ 苹果公司官网设计介绍到:Retina显示屏的超高像素密度已超过人眼能分辨的范围.Re ...
- iOS 图片的解压缩
一.图片加载的工作流 概括来说,从磁盘中加载一张图片,并将它显示到屏幕上,中间的主要工作流如下: 假设我们使用 +imageWithContentsOfFile: 方法从磁盘中加载一张图片,此时的图片 ...
- UIImage 和 iOS 图片压缩UIImage / UIImageVIew
UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...
- iOS 图片加载框架- SDWebImage 解读
在iOS的图片加载框架中,SDWebImage可谓是占据大半壁江山.它支持从网络中下载且缓存图片,并设置图片到对应的UIImageView控件或者UIButton控件.在项目中使用SDWebImage ...
- iOS图片缓存框架SDWebImage
本文转发至: http://blog.csdn.net/uxyheaven/article/details/7909373 http://www.cocoachina.com/ios/20141212 ...
- 实现iOS图片等资源文件的热更新化(三):动态的资源文件夹
简介 此文,将尝试动态从某个不确定的文件夹中加载资源文件.文章,会继续完善自定义的 imageNamed 函数,并为下一篇文章铺垫. 这么做的意义 正如我们经常所说的那样,大多数情景知道做事的意义往往 ...
- jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)
上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取 ...
- 实现iOS图片等资源文件的热更新化(五): 一个简单完整的资源热更新页面
简介 一个简单的关于页面,有一个图片,版本号,App名称等,着重演示各个系列的文章完整集成示例. 动机与意义 这是系列文章的最后一篇.今天抽空写下,收下尾.文章本身会在第四篇的基础上,简单扩充下代码, ...
随机推荐
- 微信第一个“小程序”亮相:不是APP胜似APP!
前天晚上,微信终于推出了“小程序”功能.看过效果演示之后,网友表示,好多App可以卸载了! 据了解,微信“小程序”已首批开放给200名拥有微信服务号的开发者进行内测,而且目前开发者发布的小程序无法在用 ...
- 简单统计SQLSERVER用户数据表大小(包括记录总数和空间占用情况)
在SQLSERVER,简单的组合sp_spaceused和sp_MSforeachtable这两个存储过程,可以方便的统计出用户数据表的大小,包括记录总数和空间占用情况,非常实用,在SqlServer ...
- Git学习笔记——一个NB的分布式版本控制系统
1. 命令: git init 创建新仓库 (在一个空文件下然后执行命令) git clone + 路径 检出仓库,从本地或从服务器上 git status 查 ...
- G-nav-03
/*dele masthead.css style*/.masthead .navigation .btn.btn-masthead.btn-apply:after { content: ''; di ...
- Learn sed using these command on Linux(流线式编辑器——sed)
是对文件中的每一行进行处理,不会对源文件进行修改 sed --version sed '11d' sed_file sed -n '/[Bb]erry/p' sed_file (由于设置了n,所以只打 ...
- 【ZOJ 3609】Modular Inverse
题 题意 求a关于m的乘法逆元 分析 a x ≡ 1 (mod m) 等价于 ax+my=1 求x的最小正数(不能是0,我就WA在这里了). 当m=1时,或者 gcd(a,m)!=1 时x不存在. 所 ...
- Yii2请求,报400错误
出现400错误是yii2.0的csrf防范策略导致 在components里面添加request配置如下: 'request' => [ // !!! insert a secret key i ...
- 判断一个数据是否存在于一个表中,Oracle中写自定义函数
create or replace function isExist(data in DataTypes) --DataTypes 为表中该数据的类型return Numberisv_flag num ...
- BZOJ-4195 NOI2015Day1T1 程序自动分析 并查集+离散化
总的来说,这道题水的有点莫名奇妙,不过还好一次轻松A 4195: [Noi2015]程序自动分析 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 836 ...
- EPROCESS 进程/线程优先级 句柄表 GDT LDT 页表 《寒江独钓》内核学习笔记(2)
在学习笔记(1)中,我们学习了IRP的数据结构的相关知识,接下来我们继续来学习内核中很重要的另一批数据结构: EPROCESS/KPROCESS/PEB.把它们放到一起是因为这三个数据结构及其外延和w ...