ios图片剪切

本文介绍了在iOS应用中实现截图功能的方法,包括如何截取视图的一部分并将其显示在UIImageView中,以及如何将截图保存为JPEG和PNG格式的文件。



#import "ViewController.h"

@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    UIImage *image = [UIImage imageNamed:@"1"];
//    截取屏幕位置
    CGImageRef dst = CGImageCreateWithImageInRect(image.CGImage, CGRectMake(0, 0, 50, 50));
//    将截取的屏幕放在另一个view中
    _imageView.image = [UIImage imageWithCGImage:dst];
    
}
- (IBAction)paly:(id)sender {
    UIGraphicsBeginImageContext(self.view.frame.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    [self.view.layer renderInContext:context];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    _imageView.image = image;
    
    NSData *jpgData = UIImageJPEGRepresentation(image, 0);
    [jpgData writeToFile:@"/Users/apple/Desktop/test.jpg" atomically:YES];
    
    NSData *pngData = UIImagePNGRepresentation(image);
    [pngData writeToFile:@"/Users/apple/Desktop/test.png" atomically:YES];
}

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

@end

 

转载于:https://www.cnblogs.com/daijiahong/p/4183265.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值