IOS读取图片(转载)
来源:互联网 发布:egd网络黄金是否合法 编辑:程序博客网 时间:2024/04/28 01:43
一.读取图片
1.从资源(resource)读取
[cpp] viewplaincopyprint?
- UIImage*
image=[UIImage "1.jpg"];imageNamed:@
2.从网络读取
[cpp] viewplaincopyprint?
- NSURL
*url=[NSURL "http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"];URLWithString:@ - UIImage
*imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];
3.从手机本地读取
[cpp] viewplaincopyprint?
- //读取本地图片非resource
- NSString
*aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"]; - UIImage
*imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3]; - UIImageView*
imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];
[cpp] viewplaincopyprint?
- //add
ImageIO.framework and #import - CGImageSourceRef
source = CGImageSourceCreateWithU RL((CFURLRef)url, NULL); - CGImageRef
img= CGImageSourceCreateImage AtIndex(source,0,NULL); - CGContextRef
ctx=UIGraphicsGetCurrentCont ext(); - CGContextSaveGState(ctx);
- //transformCTM的2种方式
- //CGContextConcatCTM(ctx,
CGAffineTransformMakeSca le(.2, -0.2)); - //CGContextScaleCTM(ctx,1,-1);
- //注意坐标要反下,用ctx来作为图片源
- CGImageRef
capture=CGBitmapContextCreateIma ge(ctx); - CGContextDrawImage(ctx,
CGRectMake(160, 0, 160, 230), [image CGImage]); - CGContextDrawImage(ctx,
CGRectMake(160, 230, 160, 230), img); - CGImageRef
capture2=CGBitmapContextCreateIma ge(ctx);
5.用Quartz的CGImageSourceRef来读取图片
[cpp] viewplaincopyprint?
- CGImageSourceRef
source = CGImageSourceCreateWithU RL((CFURLRef)url, NULL); - CGImageRef
img= CGImageSourceCreateImage AtIndex(source,0,NULL);
二.保存图片
1.转换成NSData来保存图片(imgFromUrl是UIImage)
[cpp] viewplaincopyprint?
- //保存图片
2种获取路径都可以 - //NSArray*paths=NSSearchPathForDirectori
esInDomains(NSDocumentDirectory, NSUserDomainMask, YES); - //NSString*documentsDirectory=[paths
objectAtIndex:0]; - //NSString*aPath=[documentsDirectory
stringByAppendingPathCom ponent:[NSString stringWithFormat:@"%@.jpg",@"test"]]; - NSString
*aPath=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"]; - NSData
*imgData = UIImageJPEGRepresentatio n(imgFromUrl,0); - [imgData
writeToFile:aPath atomically:YES];
2.用Quartz的CGImageDestinationRef来输出图片,这个方式不常见,所以不做介绍,详细可以看apple文档Quartz2D Programming Guide
三.绘制图(draw|painting)
1.UIImageView方式加入到UIView层
[cpp] viewplaincopyprint?
- UIImageView*
imageView=[[UIImageView alloc]initWithImage:image]; - imageView.frame=CGRectMake(0,
0, 320, 480); - [self
addSubview:imageView]; - [imageView
release];
[cpp] viewplaincopyprint?
- [image4
drawAtPoint:CGPointMake(100, 0)];
[cpp] viewplaincopyprint?
- CGContextDrawImage(ctx,
CGRectMake(160, 0, 160, 230), [image CGImage]);
4.CGLayer
这个是apple推荐的一种offscreen的绘制方法,相比bitmapContext更好,因为它似乎会利用iphone硬件(drawing-card)加速
[cpp] viewplaincopyprint?
- CGLayerRef
cg=CGLayerCreateWithContext (ctx, CGSizeMake(320, 480), NULL); - //需要将CGLayerContext来作为缓存context,这个是必须的
- CGContextRef
layerContext=CGLayerGetContext(cg); - CGContextDrawImage(layerContext,
CGRectMake(160, 230, 160, 230), img); - CGContextDrawLayerAtPoin
t(ctx, CGPointMake(0, 0), cg);
5.CALayer的contents
[cpp] viewplaincopyprint?
- UIImage*
image=[UIImage "1.jpg"];imageNamed:@ - CALayer
*ly=[CALayer layer]; - ly.frame=CGRectMake(0,
0, 320, 460); - ly.contents=[image
CGImage]; - [self.layer
addSublayer:ly];
四.其它
1.CGImage和UIImage互换
这样就可以随时切换UIKit和Quartz之间类型,并且选择您熟悉的方式来处理图片.
CGImage cgImage=[uiImage CGImage];
UIImage* uiImage=[UIImageimageWithCGImage:cgImage];
0 0
- IOS读取图片(转载)
- 读取本地图片并预览(转载)
- iOS中读取图片
- ios 关于图片读取
- ios 读取图片
- 数据库中图片存储及读取 (转载)
- PyTorch读取Cifar数据集并显示图片(转载)
- ios读取网络图片数据
- iOS 读取图片 exif 信息
- iOS 二维码 (包括扫描二维码,和读取二维码图片)
- iPhone/iOS图片读取、保存、绘制
- IOS SQLite3 存储和读取图片
- Unity3D读取Ios中的动态UIImage图片
- ios 读取数据库中的图片方法
- ios 实现图片读取,保存,绘制
- ios从系统相册中读取图片
- iOS 读取图片的各种方法
- iOS 读取图片的各种方法
- Mysql常用功能操作实现
- java基础
- Android 开发第六弹:简易时钟(计时器)
- 《Hadoop: The Definitive Guide》读书笔记 -- Chapter 3 The Hadoop distributed Filesystem
- Android 5.1 Contacts源码分析(四):Contacts模块ListView Adapter结构
- IOS读取图片(转载)
- ibatis配置方法
- Android DownloadManager下载状态查询(2)
- 217.Contains Duplicate
- Eclipse JEE & ADE & Git & SVN & AXIS2/ Wtp
- 什么是BNF范式,什么又是EBNF范式? http://blog.csdn.net/designlife/article/details/3206543
- TDDL配置方法(数据源bean,分库分表,sequence)
- 数据库事务隔离级别与索引的优化
- 144.Binary Tree Preorder Traversal