现在的位置: 首页 > 综合 > 正文

ios 图片的截取

2018年04月22日 ⁄ 综合 ⁄ 共 5035字 ⁄ 字号 评论关闭

第一种:整个屏幕截取

在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

抱歉!评论已关闭.