歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> 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];
 }

Copyright © Linux教程網 All Rights Reserved