Welcome 微信登录
编程资源 图片资源库 蚂蚁家优选 PDF转换器

首页 / 操作系统 / Linux / iOS 图片的截取

iOS 图片的截取第一种:整个屏幕截取
 
在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];
 }