ios 图片的截取
2013-04-18 16:52
417 查看
第一种:整个屏幕截取
在ViewController中写的,这个类是一个视图控制器
-(void)loadView{
//静态方法sharedApplication
[[UIApplication sharedApplication]setStatusBarHidden:YES //把状态栏隐藏
withAnimation:UIStatusBarAnimationSlide];
UIImage *mage=[UIImage imageNamed:@"image.png"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
//UIImageView *im=[UIImageView alloc]initWithImage:mage];
[imageView setImage:mage];
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:contentView];
CGRect rect=CGRectMake(0, 0, 320, 480);
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext=UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContext,rect);
CGContextDrawImage(currentContext, rect, mage.CGImage);
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
contentView.image=image;
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:contentView];
[image release];
}
第二种:整张图片的缩略
-(void)loadView{
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"image.png"];
//UIImageView *contentView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//用来控制图片缩到多大
CGRect rect=CGRectMake(0, 0, 160, 240);
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContent=UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContent, rect);
[image drawInRect:rect];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *contentView=[[UIImageView alloc]initWithFrame:rect];
contentView.image=image;
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:contentView];
[image release];
}
第san种:真正的截取图片
- (void)loadView {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"image2.png"];
CGRect rect = CGRectMake(33, 22, 140, 353);//创建矩形框
CGRect re=CGRectMake(0, 0, 140, 140);
UIImageView *contentView = [[UIImageView alloc] initWithFrame:re];
contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect)];
self.view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
ImageView *imageView=[[ImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor=[UIColor yellowColor];
[self.view addSubview:imageView];
[self.view addSubview:contentView];
[image release];
}
******************************以下是写的一个简单的Demo******************************
#import <UIKit/UIKit.h>
@interface imageView : UIView{
CGPoint starPoint;
CGPoint endPoint;
UIImage *image;
}
@end
#import "imageView.h"
@implementation imageView
-(void)dealloc{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
allMovePoints=[[NSMutableArray alloc]init];
//self.frame=CGRectMake(0, 0, 220, 340);
self.frame=CGRectMake(0, 0, 320, 480);
image=[UIImage imageNamed:@"image.png"];
self.backgroundColor=[UIColor colorWithPatternImage:image];
// UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
// imageView.tag=3;
// imageView.frame=CGRectMake(0, 0, 320, 360);
// [self addSubview:imageView];
// Initialization code
}
return self;
}
-(void)drawRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1, 5.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(starPoint.x, starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y));
CGContextStrokePath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t=[touches anyObject];
starPoint=[t locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t=[touches anyObject];
//CGPoint p=[t locationInView:self];
endPoint=[t locationInView:self];
UIImageView *image11=(UIImageView *)[self viewWithTag:3];
[image11 removeFromSuperview];
//[allMovePoints addObject:NSStringFromCGPoint(p)];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect rect1 = CGRectMake(starPoint.x, starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y);//创建矩形框
CGRect re=CGRectMake(320-endPoint.x+starPoint.x, 480-endPoint.y+starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y);
UIImageView *contentView = [[UIImageView alloc] initWithFrame:re];
contentView.tag=3;
contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect1)];
[self addSubview:contentView];
}
@end
在ViewController中写的,这个类是一个视图控制器
-(void)loadView{
//静态方法sharedApplication
[[UIApplication sharedApplication]setStatusBarHidden:YES //把状态栏隐藏
withAnimation:UIStatusBarAnimationSlide];
UIImage *mage=[UIImage imageNamed:@"image.png"];
UIImageView *imageView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
//UIImageView *im=[UIImageView alloc]initWithImage:mage];
[imageView setImage:mage];
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:contentView];
CGRect rect=CGRectMake(0, 0, 320, 480);
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContext=UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContext,rect);
CGContextDrawImage(currentContext, rect, mage.CGImage);
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
contentView.image=image;
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:contentView];
[image release];
}
第二种:整张图片的缩略
-(void)loadView{
[[UIApplication sharedApplication]setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"image.png"];
//UIImageView *contentView=[[UIImageView alloc]initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//用来控制图片缩到多大
CGRect rect=CGRectMake(0, 0, 160, 240);
UIGraphicsBeginImageContext(rect.size);
CGContextRef currentContent=UIGraphicsGetCurrentContext();
CGContextClipToRect(currentContent, rect);
[image drawInRect:rect];
UIImage *image=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageView *contentView=[[UIImageView alloc]initWithFrame:rect];
contentView.image=image;
self.view=[[UIView alloc]initWithFrame:[[UIScreen mainScreen]applicationFrame]];
[self.view addSubview:contentView];
[image release];
}
第san种:真正的截取图片
- (void)loadView {
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation: UIStatusBarAnimationSlide];
UIImage *image=[UIImage imageNamed:@"image2.png"];
CGRect rect = CGRectMake(33, 22, 140, 353);//创建矩形框
CGRect re=CGRectMake(0, 0, 140, 140);
UIImageView *contentView = [[UIImageView alloc] initWithFrame:re];
contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect)];
self.view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
ImageView *imageView=[[ImageView alloc]initWithFrame:CGRectMake(0, 0, 320, 480)];
self.view.backgroundColor=[UIColor yellowColor];
[self.view addSubview:imageView];
[self.view addSubview:contentView];
[image release];
}
******************************以下是写的一个简单的Demo******************************
#import <UIKit/UIKit.h>
@interface imageView : UIView{
CGPoint starPoint;
CGPoint endPoint;
UIImage *image;
}
@end
#import "imageView.h"
@implementation imageView
-(void)dealloc{
[super dealloc];
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
allMovePoints=[[NSMutableArray alloc]init];
//self.frame=CGRectMake(0, 0, 220, 340);
self.frame=CGRectMake(0, 0, 320, 480);
image=[UIImage imageNamed:@"image.png"];
self.backgroundColor=[UIColor colorWithPatternImage:image];
// UIImageView *imageView=[[UIImageView alloc]initWithImage:image];
// imageView.tag=3;
// imageView.frame=CGRectMake(0, 0, 320, 360);
// [self addSubview:imageView];
// Initialization code
}
return self;
}
-(void)drawRect:(CGRect)rect{
CGContextRef context=UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context, 1, 5.0, 1.0, 1.0);
CGContextSetLineWidth(context, 2.0);
CGContextAddRect(context, CGRectMake(starPoint.x, starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y));
CGContextStrokePath(context);
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t=[touches anyObject];
starPoint=[t locationInView:self];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
UITouch *t=[touches anyObject];
//CGPoint p=[t locationInView:self];
endPoint=[t locationInView:self];
UIImageView *image11=(UIImageView *)[self viewWithTag:3];
[image11 removeFromSuperview];
//[allMovePoints addObject:NSStringFromCGPoint(p)];
[self setNeedsDisplay];
}
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
CGRect rect1 = CGRectMake(starPoint.x, starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y);//创建矩形框
CGRect re=CGRectMake(320-endPoint.x+starPoint.x, 480-endPoint.y+starPoint.y, endPoint.x-starPoint.x, endPoint.y-starPoint.y);
UIImageView *contentView = [[UIImageView alloc] initWithFrame:re];
contentView.tag=3;
contentView.image=[UIImage imageWithCGImage:CGImageCreateWithImageInRect([image CGImage], rect1)];
[self addSubview:contentView];
}
@end
相关文章推荐
- iOS 怎样从图片中截取出需要的部分 缩放与选取
- IOS 截取任意图片
- iOS 截取原型图片
- 截取部分图片并显示-ios例子[转载]
- ios 关于UIImageView的显示问题——居中显示或者截取图片的中间部分显示
- iOS 截取部分图片并显示
- iOS截取图片
- iOS 截取图片,并且保持图片原有比例,同时填充imageView
- iOS 图片相关操作,如:翻转、缩放、截取等
- iOS - 常用到的图片功能:截取当前屏幕/选择本地图片/图片压缩
- iOS 图片截取
- iOS 截取屏幕和获取图片的一部分
- iOS截取视频某一帧图片(关键帧,AVAssetImageGenerator)
- iOS 截取屏幕当前显示的图片(可按范围截取)
- iOS 图片截取 截屏
- iOS 图片 保存到沙盒路径/相册、图片缩小、图片截取
- iOS中截取屏幕中局部图片
- iOS的截屏,图片截取以及pixel,point,scale的研究
- iOS 从图片截取图片,类似Window bitblt
- IOS 图片 截取 和 合并