美文网首页
【iOS】截屏、保存图片到相册

【iOS】截屏、保存图片到相册

作者: 794f64d7a292 | 来源:发表于2017-03-31 12:02 被阅读10063次

    为了简便我们的开发,通常会建立一个分类来处理我们的程序,废话不多说,直接上代码:

    #import <UIKit/UIKit.h>
    
    typedef enum : NSUInteger {
        ImageRotationAngleZero = 0,//逆时针旋转0°
        ImageRotationAngleM_PI_2 = 90,//逆时针旋转90°
        ImageRotationAngleM_PI = 180,//逆时针旋转180°
        ImageRotationAngle3M_PI_2 = 270//逆时针旋转270°
    } ImageRotationAngle;
    
    @interface UIImage (SnipImage)
    
    /**
     缩放并保存图片到相册
    
     @param imagePath 图片地址
     */
    + (void)scaleAndSaveImage:(NSString *)imagePath;
    
    /**
     截屏并保存
     */
    + (void)snipAndSaveImage;
    
    @end
    

    #import "UIImage+SnipImage.h"
    
    @implementation UIImage (SnipImage)
    
    //截屏并裁剪保存图片
    + (void)snipAndSaveImage{
        CGSize imageSize = [[UIScreen mainScreen] bounds].size;
        
        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 *snipImage = UIGraphicsGetImageFromCurrentImageContext();
        
        UIGraphicsEndImageContext();
        
        UIImage *saveImage = [self scaleImage:snipImage];
        UIImageWriteToSavedPhotosAlbum(saveImage,self, nil, nil);
    }
    
    //根据图片地址裁剪并保存图片
    + (void)scaleAndSaveImage:(NSString *)imagePath{
        UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
        
        UIImage *saveImage = [self scaleImage:image];
        
        UIImageWriteToSavedPhotosAlbum(saveImage,self, nil, nil);
    }
    
    //裁剪图片(此处容易有坑 *所有的尺寸应该是分辨率)
    + (UIImage *)scaleImage:(UIImage *)image{
        CGFloat screenPX_width = [UIScreen mainScreen].bounds.size.width*2;
        CGFloat screenPX_height = [UIScreen mainScreen].bounds.size.height*2;
        
        //裁剪区域根据需求自己处理
        CGFloat scaleImage_offset = 10;
        CGFloat scaleImage_height = screenPX_height-30*2;
        CGFloat scaleImage_width = screenPX_width * 0.635;
        CGFloat scaleImage_x = (screenPX_width - scaleImage_width)/2+scaleImage_offset;
        CGFloat scaleImage_y = 30;
        
        UIImage *scaleImage = [UIImage imageWithCGImage:CGImageCreateWithImageInRect(image.CGImage, CGRectMake(scaleImage_x, scaleImage_y, scaleImage_width, scaleImage_height))];
        UIImage *saveImage = [self rotationImage:scaleImage angle:270];
        return saveImage;
    }
    
    //旋转图片到指定角度
    + (UIImage *)rotationImage:(UIImage *)image angle:(ImageRotationAngle)angle{
        long double rotate = 0.0;
        CGRect rect;
        float translateX = 0;
        float translateY = 0;
        float scaleX = 1.0;
        float scaleY = 1.0;
        
        switch (angle) {
            case ImageRotationAngleZero:
                return image;
                break;
            
            case ImageRotationAngleM_PI_2:
                rotate = M_PI_2;
                rect = CGRectMake(0, 0, image.size.height, image.size.width);
                translateX = 0;
                translateY = -rect.size.width;
                scaleY = rect.size.width/rect.size.height;
                scaleX = rect.size.height/rect.size.width;
                break;
                
            case ImageRotationAngleM_PI:
                rotate = M_PI;
                rect = CGRectMake(0, 0, image.size.width, image.size.height);
                translateX = -rect.size.width;
                translateY = -rect.size.height;
                break;
                
            case ImageRotationAngle3M_PI_2:
                rotate = 3 * M_PI_2;
                rect = CGRectMake(0, 0, image.size.height, image.size.width);
                translateX = -rect.size.height;
                translateY = 0;
                scaleY = rect.size.width/rect.size.height;
                scaleX = rect.size.height/rect.size.width;
                break;
                
            default:
                break;
        }
        
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(context, 0.0, rect.size.height);
        CGContextScaleCTM(context, 1.0, -1.0);
        CGContextRotateCTM(context, rotate);
        CGContextTranslateCTM(context, translateX, translateY);
        
        CGContextScaleCTM(context, scaleX, scaleY);
        CGContextDrawImage(context, CGRectMake(0, 0, rect.size.width, rect.size.height), image.CGImage);
        
        UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
        return newImage;
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:【iOS】截屏、保存图片到相册

          本文链接:https://www.haomeiwen.com/subject/epzmottx.html