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];
- //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];
- [image4 drawAtPoint:CGPointMake(100, 0)];
- 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];
四.其它
2.UIImage resizableImageWithCapInsets的问题
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;
- }
问题描述主要是有一个很小的叉按钮,需要响应很大的点击区域,这个其实很简单,代码如下:
- 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;
- }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。