毛玻璃效果,保存图片到相册,截图

//

//  mainViewController.m

//  photofuzzy

//

//  Created by Christopher on 17/6/7.

//  Copyright © 2017 ZTracy. All rights reserved.

//

#import "mainViewController.h"

#import <Photos/Photos.h>

@interface mainViewController ()<UITableViewDataSource,UITableViewDelegate,UIImagePickerControllerDelegate, UIPickerViewDelegate,UINavigationControllerDelegate>

{

    //背景view,模态视图效果

    UIView *DoModalView;

    UIImagePickerController *imagePicker;

    UIImage *currentImage;

    

    UIBlurEffect * blur;

    UIVisualEffectView * effe;

}

@property (strong, nonatomic) IBOutlet UIImageView *selectedImg;

@property (strong, nonatomic) IBOutlet UISlider *mySlider;


@end


@implementation mainViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    

    _mySlider.minimumValue = 0;//设置最小值

    _mySlider.maximumValue = 1;//设置最大值

    _mySlider.value = 0.5;//设置默认值

    

    _mySlider.minimumTrackTintColor = [UIColor redColor];

    _mySlider.maximumTrackTintColor = [UIColor yellowColor];

    _mySlider.thumbTintColor = [UIColor purpleColor];

    

    [self performSelector:@selector(showcamera) withObject:nil afterDelay:0.3];

}

-(void)showcamera

{

    imagePicker = [[UIImagePickerController alloc] init];

    imagePicker.delegate = self;

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)selectPhoto:(UIButton *)sender {

    

    DoModalView = [[UIView alloc]initWithFrame:[UIScreen mainScreen].bounds];

    DoModalView.backgroundColor = [UIColor colorWithRed:0.4 green:0.4 blue:0.4 alpha:0.4];

    

    CGRect fram = [UIScreen mainScreen].bounds;

    fram.origin.y = self.view.bounds.size.height - 132;

    fram.size.height = 132;

    UITableView *photographTabview = [[UITableView alloc]initWithFrame:fram];

    

    photographTabview.delegate = self;

    photographTabview.dataSource = self;

    photographTabview.scrollEnabled = NO;

    [DoModalView addSubview:photographTabview];

    [self.view addSubview:DoModalView];

    

}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

    return 3;

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath

{

    return 45.0;

}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

    static NSString *cellIdentifier = @"Cell";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];

        if (cell == nil) {

            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];

        }

        cell.textLabel.textAlignment=NSTextAlignmentCenter;

        cell.textLabel.textColor = [UIColor darkGrayColor];

        switch (indexPath.row) {

            case 0:

                cell.textLabel.text = @"拍照" ;

                break;

            case 1:

                cell.textLabel.text = @"从相册中选择" ;

                break;

            case 2:

                cell.textLabel.text = @"取消" ;

                break;

            default:

                break;

        }

        return cell;

}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

        switch (indexPath.row) {

            case 0:

                [imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];

                [imagePicker setAllowsEditing:YES];

                [self presentViewController:imagePicker animated:YES completion:^{

                }];

                

                break;

            case 1:

                [imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];

                [imagePicker setAllowsEditing:YES];

                [self presentViewController:imagePicker animated:YES completion:^{

                }];

                break;

            case 2:

                [tableView removeFromSuperview];

                [DoModalView removeFromSuperview];

                break;

            default:

                break;

        }

}

#pragma mark UIImagePickerControllerDelegate

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [DoModalView removeFromSuperview];

    [imagePicker dismissViewControllerAnimated:YES completion:^{

    }];

}

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo

{

    [DoModalView removeFromSuperview];

    currentImage = [self squareImageFromImage:image scaledToSize:image.size.width];

    _selectedImg.image = currentImage;

     [picker dismissViewControllerAnimated:YES completion:^{}];

    [self fuzzyImg:0.5];

}

- (UIImage *)squareImageFromImage:(UIImage *)image scaledToSize:(CGFloat)newSize {

    CGAffineTransform scaleTransform;

    CGPoint origin;

    

    if (image.size.width > image.size.height) {

        //image原始高度为200,缩放image的高度为400pixels,所以缩放比率为2

        CGFloat scaleRatio = newSize / image.size.height;

        scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);

        //设置绘制原始图片的画笔坐标为CGPoint(-100, 0)pixels

        origin = CGPointMake(-(image.size.width - image.size.height) / 2.0f, 0);

    } else {

        CGFloat scaleRatio = newSize / image.size.width;

        scaleTransform = CGAffineTransformMakeScale(scaleRatio, scaleRatio);

        

        origin = CGPointMake(0, -(image.size.height - image.size.width) / 2.0f);

    }

    

    CGSize size = CGSizeMake(newSize, newSize);

    //创建画板为(400x400)pixels

    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) {

        UIGraphicsBeginImageContextWithOptions(size, YES, 0);

    } else {

        UIGraphicsBeginImageContext(size);

    }

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    //image原始图片(400x200)pixels缩放为(800x400)pixels

    CGContextConcatCTM(context, scaleTransform);

    //origin也会从原始(-100, 0)缩放到(-200, 0)

    [image drawAtPoint:origin];

    

    //获取缩放后剪切的image图片

    image = UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    

    return image;

}

-(void)fuzzyImg:(float)value

{

    //创建毛玻璃

    if(!blur)

    {

        blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleProminent];

        effe = [[UIVisualEffectView alloc]initWithEffect:blur];

    }

    CGRect fram = _selectedImg.frame;

    fram.origin.x = 0;

    fram.origin.y = 0;

    

    effe.frame = fram;

    effe.layer.masksToBounds = YES;

    effe.layer.cornerRadius = 0;

    effe.alpha = value;

    [_selectedImg addSubview:effe];

}

- (IBAction)SaveImageBtnClick:(UIButton *)sender {

    

    UIImage *image = [self screenshots];

    [self saveImage:image toCollectionWithName:@"myphotos"];

}

- (IBAction)SliderChanged:(UISlider *)sender {

    if ([sender isKindOfClass:[UISlider class]]) {

        UISlider * slider = sender;

        CGFloat value = slider.value;

        [self fuzzyImg:value];

    }

}

-(UIImage*)screenshots

{

    CGSize imageSize = CGSizeMake(self.view.frame.size.width,self.view.frame.size.height);//你要的截图的位置

    

    if (NULL != &UIGraphicsBeginImageContextWithOptions) {

        UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);

    }

    

    CGContextRef context = UIGraphicsGetCurrentContext();

    

    for (UIWindow *window in [[UIApplication sharedApplication] windows]) {

        if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen]) {

            CGContextSaveGState(context);

            

            CGContextTranslateCTM(context, [window center].x, [window center].y);

            

            CGContextConcatCTM(context, [window transform]);

            

            CGContextTranslateCTM(context,

                                  -[window bounds].size.width * [[window layer] anchorPoint].x,

                                  -[window bounds].size.height * [[window layer] anchorPoint].y);

            

            [[window layer] renderInContext:context];

            

            CGContextRestoreGState(context);

        }

    }

    

     UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

    

    UIGraphicsEndImageContext();

    return image;

}

// 保存图片

-(void)saveImage:(UIImage *)image toCollectionWithName:(NSString *)collectionName {

    

    // 1. 获取相片库对象

    PHPhotoLibrary *library = [PHPhotoLibrary sharedPhotoLibrary];

    

    // 2. 调用changeBlock

    [library performChanges:^{

        

        // 2.1 创建一个相册变动请求

        PHAssetCollectionChangeRequest *collectionRequest;

        

        // 2.2 取出指定名称的相册

        PHAssetCollection *assetCollection = [self getCurrentPhotoCollectionWithTitle:collectionName];

        

        // 2.3 判断相册是否存在

        if (assetCollection) { // 如果存在就使用当前的相册创建相册请求

            collectionRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];

        } else { // 如果不存在, 就创建一个新的相册请求

            collectionRequest = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:collectionName];

        }

        

        // 2.4 根据传入的相片, 创建相片变动请求

        PHAssetChangeRequest *assetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];

        

        // 2.4 创建一个占位对象

        PHObjectPlaceholder *placeholder = [assetRequest placeholderForCreatedAsset];

        

        // 2.5 将占位对象添加到相册请求中

        [collectionRequest addAssets:@[placeholder]];

        

    } completionHandler:^(BOOL success, NSError * _Nullable error) {

        

        // 3. 判断是否出错, 如果报错, 声明保存不成功

        if (error) {

            NSLog(@"保存失败");

        } else {

            NSLog(@"保存成功");


        }

    }];

}

-(PHAssetCollection *)getCurrentPhotoCollectionWithTitle:(NSString *)collectionName {

    

    // 1. 创建搜索集合

    PHFetchResult *result = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];

    

    // 2. 遍历搜索集合并取出对应的相册

    for (PHAssetCollection *assetCollection in result) {

        

        if ([assetCollection.localizedTitle containsString:collectionName]) {

            return assetCollection;

        }

    }

    

    return nil;

}

@end


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值