首页
Web开发
Windows程序
编程语言
数据库
移动开发
系统相关
微信
其他好文
会员
首页
>
移动开发
> 详细
iOS开发——UI篇OC篇&初始化图片方式
时间:
2015-06-09 23:26:57
阅读:
158
评论:
0
收藏:
0
[点我收藏+]
标签:
初始化图片方式
一.读取图片
1.从资源(resource)读取
[cpp]
view plain
copy
print
?
UIImage* image=[UIImage imageNamed:@
"1.jpg"
];
2.从网络读取
[cpp]
view plain
copy
print
?
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]];
3.从手机本地读取
[cpp]
view plain
copy
print
?
//读取本地图片非resource
NSString *aPath3=[NSString stringWithFormat:@
"%@/Documents/%@.jpg"
,NSHomeDirectory(),@
"test"
];
UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];
4.从现有的context中获得图像
[cpp]
view plain
copy
print
?
//add ImageIO.framework and #import
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来读取图片
[cpp]
view plain
copy
print
?
CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
二.保存图片
1.转换成NSData来保存图片(imgFromUrl是UIImage)
[cpp]
view plain
copy
print
?
//保存图片 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];
2.用Quartz的CGImageDestinationRef来输出图片,这个方式不常见,所以不做介绍,详细可以看apple文档Quartz 2D Programming Guide
三.绘制图(draw|painting)
1.UIImageView方式加入到UIView层
[cpp]
view plain
copy
print
?
UIImageView* imageView=[[UIImageView alloc]initWithImage:image];
imageView.frame=CGRectMake(0, 0, 320, 480);
[self addSubview:imageView];
[imageView release];
2.[img drawAtPoint]系列方法
[cpp]
view plain
copy
print
?
[image4 drawAtPoint:CGPointMake(100, 0)];
3.CGContextDrawImage
[cpp]
view plain
copy
print
?
CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
4.CGLayer
这个是apple推荐的一种offscreen的绘制方法,相比bitmapContext更好,因为它似乎会利用iphone硬件(drawing-card)加速
[cpp]
view plain
copy
print
?
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
[cpp]
view plain
copy
print
?
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];
四.其它
1.CGImage和UIImage互换
这样就可以随时切换UIKit和Quartz之间类型,并且选择您熟悉的方式来处理图片.
CGImage cgImage=[uiImage CGImage];
UIImage* uiImage=[UIImage imageWithCGImage:cgImage];
iOS开发——UI篇OC篇&初始化图片方式
标签:
原文地址:http://www.cnblogs.com/iCocos/p/4564773.html
踩
(
0
)
赞
(
0
)
举报
评论
一句话评论(
0
)
登录后才能评论!
分享档案
更多>
2021年07月29日 (22)
2021年07月28日 (40)
2021年07月27日 (32)
2021年07月26日 (79)
2021年07月23日 (29)
2021年07月22日 (30)
2021年07月21日 (42)
2021年07月20日 (16)
2021年07月19日 (90)
2021年07月16日 (35)
周排行
更多
关闭苹果IOS app自动更新
2021-07-29
开发一个即时通讯App
2021-07-28
iOS 跳转App Store进行评分
2021-07-26
诺基亚短信生成!太好玩了
2021-07-26
【Azure 应用服务】App Service 配置 Application Settings 访问Storage Account得到 could not be resolved: '*.file.core.windows.net'的报错。没有解析成对应中国区 Storage Account地址 *.file.core.chinacloudapi.cn
2021-07-26
Android系统编程入门系列之界面Activity响应丝滑的传统动画
2021-07-26
uniapp h5,app两端复制文本
2021-07-22
uni-app滚动视图容器(scroll-view)之监听上拉事件
2021-07-21
新型横向移动工具原理分析、代码分析、优缺点以及检测方案
2021-07-19
Android系统编程入门系列之界面Activity交互响应
2021-07-19
友情链接
兰亭集智
国之画
百度统计
站长统计
阿里云
chrome插件
新版天听网
关于我们
-
联系我们
-
留言反馈
© 2014
mamicode.com
版权所有 联系我们:gaon5@hotmail.com
迷上了代码!