http://blog.csdn.net/jerryvon/article/details/7526147

20:50:42

一.读取图片

1.从资源(resource)读取
  1. UIImage* image=[UIImage imageNamed:@"1.jpg"];
2.从网络读取
  1. NSURL *url=[NSURL URLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"];
  2. UIImage *imgFromUrl =[[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:url]];
3.从手机本地读取
  1. //读取本地图片非resource
  2. NSString *aPath3=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
  3. UIImage *imgFromUrl3=[[UIImage alloc]initWithContentsOfFile:aPath3];
  4. UIImageView* imageView3=[[UIImageView alloc]initWithImage:imgFromUrl3];
 

4.从现有的context中获得图像

  1. //add ImageIO.framework and #import <ImageIO/ImageIO.h>
  2. CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
  3. CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
  4. CGContextRef ctx=UIGraphicsGetCurrentContext();
  5. CGContextSaveGState(ctx);
  6. //transformCTM的2种方式
  7. //CGContextConcatCTM(ctx, CGAffineTransformMakeScale(.2, -0.2));
  8. //CGContextScaleCTM(ctx,1,-1);
  9. //注意坐标要反下,用ctx来作为图片源
  10. CGImageRef capture=CGBitmapContextCreateImage(ctx);
  11. CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);
  12. CGContextDrawImage(ctx, CGRectMake(160, 230, 160, 230), img);
  13. CGImageRef capture2=CGBitmapContextCreateImage(ctx);

5.用Quartz的CGImageSourceRef来读取图片

  1. CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef)url, NULL);
  2. CGImageRef img= CGImageSourceCreateImageAtIndex(source,0,NULL);
 
 

二.保存图片

1.转换成NSData来保存图片(imgFromUrl是UIImage)
  1. //保存图片 2种获取路径都可以
  2. //NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  3. //NSString*documentsDirectory=[paths objectAtIndex:0];
  4. //NSString*aPath=[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg",@"test"]];
  5. NSString *aPath=[NSString stringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
  6. NSData *imgData = UIImageJPEGRepresentation(imgFromUrl,0);
  7. [imgData writeToFile:aPath atomically:YES];
2.用Quartz的CGImageDestinationRef来输出图片,这个方式不常见,所以不做介绍,详细可以看apple文档Quartz 2D Programming Guide
 
 

三.绘制图(draw|painting)

1.UIImageView方式加入到UIView层
  1. UIImageView* imageView=[[UIImageView alloc]initWithImage:image];
  2. imageView.frame=CGRectMake(0, 0, 320, 480);
  3. [self addSubview:imageView];
  4. [imageView release];
 

2.[img drawAtPoint]系列方法

  1. [image4 drawAtPoint:CGPointMake(100, 0)];
 

3.CGContextDrawImage

  1. CGContextDrawImage(ctx, CGRectMake(160, 0, 160, 230), [image CGImage]);

4.CGLayer

这个是apple推荐的一种offscreen的绘制方法,相比bitmapContext更好,因为它似乎会利用iphone硬件(drawing-card)加速
  1. CGLayerRef cg=CGLayerCreateWithContext(ctx, CGSizeMake(320, 480), NULL);
  2. //需要将CGLayerContext来作为缓存context,这个是必须的
  3. CGContextRef layerContext=CGLayerGetContext(cg);
  4. CGContextDrawImage(layerContext, CGRectMake(160, 230, 160, 230), img);
  5. CGContextDrawLayerAtPoint(ctx, CGPointMake(0, 0), cg);

5.CALayer的contents

  1. UIImage* image=[UIImage imageNamed:@"1.jpg"];
  2. CALayer *ly=[CALayer layer];
  3. ly.frame=CGRectMake(0, 0, 320, 460);
  4. ly.contents=[image CGImage];
  5. [self.layer addSublayer:ly];

四.其它

1.CGImage和UIImage互换
这样就可以随时切换UIKit和Quartz之间类型,并且选择您熟悉的方式来处理图片.
CGImage cgImage=[uiImage CGImage];
UIImage* uiImage=[UIImage imageWithCGImage:cgImage];
2.UIImage resizableImageWithCapInsets的问题
假设一张44x29的图片,同样的Insets=UIEdgeInsetsMake(10,10,10,10)在@2x情况和非@2x情况下,表现会有不同,非@2x是OK正常的,但是如果同样尺寸的图片变成@2x,则导致在切换过渡的时候会很卡,应该是在不同的重绘导致的,表面原因是因为Insets设置的是点,在@2x情况下拉伸,其实拉升的像素是上面20,下面也是20,但是图片其实只有29,所以导致不正确,只要将insets设置成=UIEdgeInsetsMake(5,10,5,10)就正常了,所以以后要注意了。
 
3.动画图片使用注意

animationImage 设置完毕以后要startAnimation.不会自动启动动画图片。

此外在读取大量动画图片的时候不太适合用这个方法,因为一下子那么多图片容易爆掉。可以用这个方法替代,具体我也没试,方法就是手动切换图片,并非直接使用系统方法而已。

  1. imgV=[[UIImageView alloc]initWithFrame:CGRectMake(40, 40, 128, 128)];
  2. [self.window addSubview:imgV];
  3. [self performSelectorInBackground:@selector(playAnim)withObject:nil];
  4. [imgV release];
  5. -(void)playAnim{
  6. for (int i=0;i<101;){
  7. usleep(100000);
  8. UIImage *image=[[UIImage alloc]initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"%d",i+1 ] ofType:@"tiff"]];
  9. [self performSelectorOnMainThread:@selector(changeImage:) withObject:image waitUntilDone:YES];
  10. i++;
  11. }
  12. }
  13. -(void)changeImage:(UIImage*)image{
  14. imgV.image=image;
  15. }

相关帖子:http://www.cocoachina.com/bbs/read.php?tid=110154

4.UIControl设置UIImage

问题描述主要是有一个很小的叉按钮,需要响应很大的点击区域,这个其实很简单,代码如下:

  1. UIImage *bg=[UIImage imageNamed:@"heizi1.jpg"];
  2. //图片大于点及区域,缩小下就行
  3. bg=[self scaleImage:bg ToSize:(CGSize){100,100}];
  4. UIButton* button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
  5. //图片大于button,则会被拉伸,如果小于button则居中显示
  6. [button setImage:bg forState:UIControlStateNormal];

此外多说一句,这个icon图片如果要准备2套图,缩放毕竟消耗效率

缩放图片代码

  1. -(UIImage *)scaleImage:(UIImage *)img ToSize:(CGSize)itemSize{
  2. UIImage *i;
  3. //    CGSize itemSize=CGSizeMake(30, 30);
  4. UIGraphicsBeginImageContext(itemSize);
  5. CGRect imageRect=CGRectMake(0, 0, itemSize.width, itemSize.height);
  6. [img drawInRect:imageRect];
  7. i=UIGraphicsGetImageFromCurrentImageContext();
  8. UIGraphicsEndImageContext();
  9. return i;

从view截图出来

  1. #import <QuartzCore/QuartzCore.h>
  2. -(UIImage *)getImageFromView:(UIView *)orgView{
  3. UIGraphicsBeginImageContext(orgView.bounds.size);
  4. [orgView.layer renderInContext:UIGraphicsGetCurrentContext()];
  5. UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
  6. UIGraphicsEndImageContext();
  7. return image;
  8. }

iPhone/iOS图片相关(读取、保存、绘制、其它相关)的更多相关文章

  1. python图片的读取保存

    #coding:utf-8 from PIL import Image import matplotlib.pyplot as plt img=Image.open("F:\\Upan\\源 ...

  2. 大屏iPhone的适配 +iOS 图片尺寸要求

    摘自:http://blog.ibireme.com/2014/09/16/adapted_to_iphone6/ 苹果公司官网设计介绍到:Retina显示屏的超高像素密度已超过人眼能分辨的范围.Re ...

  3. iOS 图片的解压缩

    一.图片加载的工作流 概括来说,从磁盘中加载一张图片,并将它显示到屏幕上,中间的主要工作流如下: 假设我们使用 +imageWithContentsOfFile: 方法从磁盘中加载一张图片,此时的图片 ...

  4. UIImage 和 iOS 图片压缩UIImage / UIImageVIew

    UIImageView 制作气泡 stretchableImageWithLeftCapWidth http://blog.csdn.net/justinjing0612/article/detail ...

  5. iOS 图片加载框架- SDWebImage 解读

    在iOS的图片加载框架中,SDWebImage可谓是占据大半壁江山.它支持从网络中下载且缓存图片,并设置图片到对应的UIImageView控件或者UIButton控件.在项目中使用SDWebImage ...

  6. iOS图片缓存框架SDWebImage

    本文转发至: http://blog.csdn.net/uxyheaven/article/details/7909373 http://www.cocoachina.com/ios/20141212 ...

  7. 实现iOS图片等资源文件的热更新化(三):动态的资源文件夹

    简介 此文,将尝试动态从某个不确定的文件夹中加载资源文件.文章,会继续完善自定义的 imageNamed 函数,并为下一篇文章铺垫. 这么做的意义 正如我们经常所说的那样,大多数情景知道做事的意义往往 ...

  8. jquery mobile上传图片完整例子(包含ios图片横向问题处理和C#后台图片压缩)

    上传图片本身是个基本的小功能,但是到了移动端就不那么简单了,相信找到这篇文章的你一定有深深的同感. 本文实例是:在(移动端)页面中点击图片,然后选择文件,然后保存.使用Asp.net 难点一:后台获取 ...

  9. 实现iOS图片等资源文件的热更新化(五): 一个简单完整的资源热更新页面

    简介 一个简单的关于页面,有一个图片,版本号,App名称等,着重演示各个系列的文章完整集成示例. 动机与意义 这是系列文章的最后一篇.今天抽空写下,收下尾.文章本身会在第四篇的基础上,简单扩充下代码, ...

随机推荐

  1. 用html5+js实现掌机游戏赛车demo

    最近无聊,用html5+js做了一个以前玩过的掌机赛车游戏,顺便学习一下画布的api以及巩固一下js基础. 游戏界面,没做什么美化. 游戏规则:游戏界面分为三列,黑色方块随机落下,红色方块可以在三列自 ...

  2. 自己留存:mysql full text 支持中文的一个设定

    innodb_ft_min_token_size=1 ft_min_word_len=1

  3. JS中decodeURI()与decodeURIComponent()区别

    decodeURI()定义和用法:decodeURI() 函数可对 encodeURI() 函数编码过的URI 进行解码. 语法:decodeURI(URIstring) 参数 描述:URIstrin ...

  4. 02.C#可空類型、默認參數、LINQ(一章1.3-1.4)

    利用上班時間發個隨筆,不知領導會不會看到,可能會有同事看到也說不定啊:) 關于可空類型,在C#1中沒有這個概念,在C#3中引入的.那比如我們要實現一個表示人的類,人有名字和年齡兩個屬性,如何表示一個沒 ...

  5. [C#]Attribute特性(3)——AttributeUsage特性和特性标识符

    相关文章   [C#]Attribute特性 [C#]Attribute特性(2)——方法的特性及特性参数 AttributeUsage特性 除了可以定制自己的特性来注释常用的C#类型外,您可以用At ...

  6. javascript继承(二)—创建对象的三种模式

    一.工厂模式 function createPerson(name,age){ var o = {}; o.name = name; o.age = age; o.sayHi = function() ...

  7. ipvsadm参数详解(常用命令)

    [root@localhost ipvsadm]# ipvsadm -h ipvsadm v1.24 2005/12/10 (compiled with popt and IPVS v1.2.1) U ...

  8. 【转】div居中代码 DIV水平居中显示CSS代码

    原文地址:http://www.divcss5.com/rumen/r622.shtml 如何使用CSS让DIV居中显示,让div水平居中有哪些CSS样式呢? 需要的主要css代码有两个,一个为tex ...

  9. 【CodeForces 520E】Pluses everywhere

    题意 n个数里插入k个+号,所有式子的和是多少(取模1000000007) (0 ≤ k < n ≤ 105). 分析 1.求答案,考虑每个数作为i位数(可为答案贡献10的i-1次方,个位i=1 ...

  10. shell与变量的声明的操作

    1.给命令起别名:alias 执行下面命令后,可以使用dir代替ls –l 命令,显示目录中的文件详细信息: 还可以用一个别名表示几个命令 的结合: 2.ps:显示当前登录会话的所有活动进程: 3.更 ...