iPhone/iOS图片相关(读取、保存、绘制、其它相关)

一.读取图片

1.从资源(resource)读取
  1. UIImage*image=[UIImageimageNamed:@"1.jpg"];

2.从网络读取
  1. NSURL*url=[NSURLURLWithString:@"http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/15/80046_950.jpg"];
  2. UIImage*imgFromUrl=[[UIImagealloc]initWithData:[NSDatadataWithContentsOfURL:url]];

3.从手机本地读取
  1. //读取本地图片非resource
  2. NSString*aPath3=[NSStringstringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
  3. UIImage*imgFromUrl3=[[UIImagealloc]initWithContentsOfFile:aPath3];
  4. UIImageView*imageView3=[[UIImageViewalloc]initWithImage:imgFromUrl3];

4.从现有的context中获得图像
  1. //addImageIO.frameworkand#import<ImageIO/ImageIO.h>
  2. CGImageSourceRefsource=CGImageSourceCreateWithURL((CFURLRef)url,NULL);
  3. CGImageRefimg=CGImageSourceCreateImageAtIndex(source,0,NULL);
  4. CGContextRefctx=UIGraphicsGetCurrentContext();
  5. CGContextSaveGState(ctx);
  6. //transformCTM的2种方式
  7. //CGContextConcatCTM(ctx,CGAffineTransformMakeScale(.2,-0.2));
  8. //CGContextScaleCTM(ctx,1,-1);
  9. //注意坐标要反下,用ctx来作为图片源
  10. CGImageRefcapture=CGBitmapContextCreateImage(ctx);
  11. CGContextDrawImage(ctx,CGRectMake(160,0,160,230),[imageCGImage]);
  12. CGContextDrawImage(ctx,CGRectMake(160,230,160,230),img);
  13. CGImageRefcapture2=CGBitmapContextCreateImage(ctx);

5.用Quartz的CGImageSourceRef来读取图片
  1. CGImageSourceRefsource=CGImageSourceCreateWithURL((CFURLRef)url,NULL);
  2. CGImageRefimg=CGImageSourceCreateImageAtIndex(source,0,NULL);


二.保存图片

1.转换成NSData来保存图片(imgFromUrl是UIImage)
  1. //保存图片2种获取路径都可以
  2. //NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
  3. //NSString*documentsDirectory=[pathsobjectAtIndex:0];
  4. //NSString*aPath=[documentsDirectorystringByAppendingPathComponent:[NSStringstringWithFormat:@"%@.jpg",@"test"]];
  5. NSString*aPath=[NSStringstringWithFormat:@"%@/Documents/%@.jpg",NSHomeDirectory(),@"test"];
  6. NSData*imgData=UIImageJPEGRepresentation(imgFromUrl,0);
  7. [imgDatawriteToFile:aPathatomically:YES];

2.用Quartz的CGImageDestinationRef来输出图片,这个方式不常见,所以不做介绍,详细可以看apple文档Quartz 2D Programming Guide


三.绘制图(draw|painting)

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

2.[img drawAtPoint]系列方法
  1. [image4drawAtPoint:CGPointMake(100,0)];

3.CGContextDrawImage
  1. CGContextDrawImage(ctx,CGRectMake(160,0,160,230),[imageCGImage]);

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

5.CALayer的contents
  1. UIImage*image=[UIImageimageNamed:@"1.jpg"];
  2. CALayer*ly=[CALayerlayer];
  3. ly.frame=CGRectMake(0,0,320,460);
  4. ly.contents=[imageCGImage];
  5. [self.layeraddSublayer: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=[[UIImageViewalloc]initWithFrame:CGRectMake(40,40,128,128)];
  2. [self.windowaddSubview:imgV];
  3. [selfperformSelectorInBackground:@selector(playAnim)withObject:nil];
  4. [imgVrelease];
  5. -(void)playAnim{
  6. for(inti=0;i<101;){
  7. usleep(100000);
  8. UIImage*image=[[UIImagealloc]initWithContentsOfFile:[[NSBundlemainBundle]pathForResource:[NSStringstringWithFormat:@"%d",i+1]ofType:@"tiff"]];
  9. [selfperformSelectorOnMainThread:@selector(changeImage:)withObject:imagewaitUntilDone: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=[UIImageimageNamed:@"heizi1.jpg"];
  2. //图片大于点及区域,缩小下就行
  3. bg=[selfscaleImage:bgToSize:(CGSize){100,100}];
  4. UIButton*button=[[UIButtonalloc]initWithFrame:CGRectMake(0,0,200,200)];
  5. //图片大于button,则会被拉伸,如果小于button则居中显示
  6. [buttonsetImage:bgforState:UIControlStateNormal];

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

缩放图片代码

  1. -(UIImage*)scaleImage:(UIImage*)imgToSize:(CGSize)itemSize{
  2. UIImage*i;
  3. //CGSizeitemSize=CGSizeMake(30,30);
  4. UIGraphicsBeginImageContext(itemSize);
  5. CGRectimageRect=CGRectMake(0,0,itemSize.width,itemSize.height);
  6. [imgdrawInRect:imageRect];
  7. i=UIGraphicsGetImageFromCurrentImageContext();
  8. UIGraphicsEndImageContext();
  9. returni;

从view截图出来
  1. #import<QuartzCore/QuartzCore.h>
  2. -(UIImage*)getImageFromView:(UIView*)orgView{
  3. UIGraphicsBeginImageContext(orgView.bounds.size);
  4. [orgView.layerrenderInContext:UIGraphicsGetCurrentContext()];
  5. UIImage*image=UIGraphicsGetImageFromCurrentImageContext();
  6. UIGraphicsEndImageContext();
  7. returnimage;
  8. }


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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值