截屏與圖片的截屏

  • 布局圖:
  • 主要代碼:

// ViewController.h
#import <UIKit/UIKit.h>

@interface ViewController : UIViewController


@end

// ViewController.m
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageV;

@property (nonatomic, assign)CGPoint startP;

@property (nonatomic, weak) UIView *coverView;

@end

@implementation ViewController

// 懶加載:1.什么時候用到什么時候才去創建
//      2.保持當前的View在內存當中只有一份.
//      3.保持用到View時,肯定是有值的.
-(UIView *)coverView {

    if (_coverView == nil) {
        //創建UIView
        UIView *coverView = [[UIView alloc] init];
        coverView.backgroundColor = [UIColor blackColor];
        coverView.alpha = 0.7;
        _coverView = coverView;
        [self.view addSubview:coverView];
    }
    return _coverView;

}

- (IBAction)pan:(UIPanGestureRecognizer *)pan {

    //獲取當前手指所在的點
    CGPoint curP = [pan locationInView:self.imageV];
    //判斷手勢的狀態
    if(pan.state == UIGestureRecognizerStateBegan) {
        //記錄當前手指的開始點
        self.startP = curP;

    } else if(pan.state == UIGestureRecognizerStateChanged) {

        //rect
        CGFloat w = curP.x - self.startP.x;
        CGFloat h = curP.y - self.startP.y;
        CGRect rect = CGRectMake(self.startP.x, self.startP.y, w, h);

        self.coverView.frame = rect;

    } else if(pan.state == UIGestureRecognizerStateEnded) {


        // 生成一張圖片
        // 1.開啟位圖上下文
        UIGraphicsBeginImageContext(self.imageV.bounds.size);

        // 2.設置裁剪區域
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.coverView.frame];
        [path addClip];

        // 3.把UIImageV當中的內容渲染到上下文當中
        // 拿到當前上下文
        CGContextRef ctx = UIGraphicsGetCurrentContext();
        [self.imageV.layer renderInContext:ctx];

        // 4.從上下文當中獲取圖片
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

        // 5.關閉上下文
        UIGraphicsEndImageContext();

        self.imageV.image = newImage;

        [self.coverView removeFromSuperview];
    }

}



- (void)viewDidLoad {
    [super viewDidLoad];
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    // 截屏
//    [self screenCapture];
}

// 截屏
- (void)screenCapture {
    // 生成圖片
    // 1.開啟一個位圖上下文
    UIGraphicsBeginImageContext(self.view.bounds.size);
    // 拿到當前上下文,即當前位圖上下文
    CGContextRef ctx =  UIGraphicsGetCurrentContext();
    // 2.把View的內容繪制到上下文當中
    // UIView內容想要繪制到上下文當中, 必須使用渲染的方式
    [self.view.layer renderInContext:ctx];
    // 3.從上下文當中生成一張圖片
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    // 4.關閉上下文
    UIGraphicsEndImageContext();
    // 把圖片轉成二進制流
    // NSData *data = UIImageJPEGRepresentation(newImage, 1); // 1代表不壓縮
    NSData *data = UIImagePNGRepresentation(newImage);

    [data writeToFile:@"/Users/apple/Desktop/newImage.png" atomically:YES];
}

@end

  • 贈送一個福利

// UIImage+Image.h
#import <UIKit/UIKit.h>

@interface UIImage (Image)


// 根據顏色生成一張尺寸為1*1的相同顏色圖片
+ (UIImage *)imageWithColor:(UIColor *)color;


@end

// UIImage+Image.m
#import "UIImage+Image.h"

@implementation UIImage (Image)

+ (UIImage *)imageWithColor:(UIColor *)color {
    // 描述矩形
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);

    // 開啟位圖上下文
    UIGraphicsBeginImageContext(rect.size);
    // 獲取位圖上下文
    CGContextRef context = UIGraphicsGetCurrentContext();
    // 使用color演示填充上下文
    CGContextSetFillColorWithColor(context, [color CGColor]);
    // 渲染上下文
    CGContextFillRect(context, rect);
    // 從上下文中獲取圖片
    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    // 結束上下文
    UIGraphicsEndImageContext();

    return theImage;
}


@end

  • 效果:
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容