zoukankan      html  css  js  c++  java
  • iOS 截屏/将图片存储到相册或沙盒目录下

    //切图

    - (UIImage*)screenView:(UIView *)view{

        CGRect rect = view.frame;

        UIGraphicsBeginImageContext(rect.size);

        CGContextRef context = UIGraphicsGetCurrentContext();

        [view.layer renderInContext:context];

        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();

        UIGraphicsEndImageContext();

       return img;

    }

    //切图并保存到相册中

    -(void)savaImageToPhotosAlbumWithImage:(UIImage *)viewImage{

        CGImageRef imageRef = viewImage.CGImage;

        CGRect rect = CGRectMake(166, 211, 426, 320);//这里可以设置想要截图的区域

        //CGRect rect = CGRectMake(0, 0, iPadWidth, iPadHeight);//这里可以设置想要截图的区域

        CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);

        

        UIImage *sendImage = [[UIImage alloc] initWithCGImage:imageRefRect];

        UIImageWriteToSavedPhotosAlbum(sendImage, nil, nil, nil);//保存图片到照片库

    //    NSData *imageViewData = UIImagePNGRepresentation(sendImage);

        CGImageRelease(imageRefRect);

    }

    //将图片保存到沙盒目录下

    -(void)savaImageToDomainMaskWithIamge:(UIImage *)image{

        NSData *imageData = UIImagePNGRepresentation(image);

        /*

         *设置图片存储位置

         */

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentationDirectory, NSUserDomainMask, YES);

        NSString *documentsDirectory = [paths objectAtIndex:0];

        NSString *pictureName = @"picture.png";

        NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:pictureName];

        [imageData writeToFile:savedImagePath atomically:YES];//保存照片到沙盒目录

    }

  • 相关阅读:
    Tennix — 开源的网球游戏
    Tile Racer — 3D 赛车游戏
    51CTO网管生活
    分割图片的例子 回复 "小熊宝" 的问题
    图解 CSS (5): font 字体
    图解 CSS (9): 列表
    图解 CSS (11): 理解样式表的逻辑
    图解 CSS (8): 浮动、显示、隐藏
    图解 CSS (10): 链接、继承、放缩、鼠标指针、其他(待补充...)
    多线程编程(2) 从 CreateThread 说起
  • 原文地址:https://www.cnblogs.com/wj0920wjx/p/5113182.html
Copyright © 2011-2022 走看看