zoukankan      html  css  js  c++  java
  • UIKit

    UIImage 是最常用的图像数据类型,通常搭配UIImageView一起使用。

    打开xcode里面的UIImage.h ,可以看到常用的方法:

    UIImagePNGRepresentation() 把一个UIImage 转成 NSdata。(里面的数据是png)

    UIImageJPEGRepresentation() 把一个UIImage 转成 NSData。(里面的数据是jpeg)

     

    + (nullable UIImage *)imageNamed:(NSString *)name;            通过name返回一张图片;

    + (nullable UIImage *)imageWithContentsOfFile:(NSString *)path;    通过path返回一张图片;

    + (nullable UIImage *)imageWithData:(NSData *)data;         通过data返回一张图片;

     

     

     

    + (nullable UIImage *)animatedImageNamed:(NSString *)name duration:(NSTimeInterval)duration;

    + (nullable UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets duration:(NSTimeInterval)duration;

    + (nullable UIImage *)animatedResizableImageNamed:(NSString *)name capInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode duration:(NSTimeInterval)duration;

    + (nullable UIImage *)animatedImageWithImages:(NSArray<UIImage *> *)images duration:(NSTimeInterval)duration;

    这四个都是返回动态图片。

     

     

     

    - (UIImage *)imageWithAlignmentRectInsets:(UIEdgeInsets)alignmentInsets;   这个是用来做九宫格图片。

     

     

    从iOS中选择图片  可以使用  UIImagePickerController;

    把照片存入库       可以使用  UIImageWriteToSavedPhotosAlbum。

    这些常用方法,已经可以满足绝大部分需求。

     

    下面举一个UIImagePickerController的例子:

     

    //当选择一张图片后进入这里

    -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

     

    {

        NSString *type = [info objectForKey:UIImagePickerControllerMediaType];

        

        //当选择的类型是图片

        if ([type isEqualToString:@"public.image"])

        {

            //先把图片转成NSData

            UIImage* image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

            NSData *data;

            if (UIImagePNGRepresentation(image) == nil)

            {

                data = UIImageJPEGRepresentation(image, 1.0);

            }

            else

            {

                data = UIImagePNGRepresentation(image);

            }

            

            //图片保存的路径

            //这里将图片放在沙盒的documents文件夹中

            NSString * DocumentsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];

            

            //文件管理器

            NSFileManager *fileManager = [NSFileManager defaultManager];

            

            //把刚刚图片转换的data对象拷贝至沙盒中 并保存为image.png

            [fileManager createDirectoryAtPath:DocumentsPath withIntermediateDirectories:YES attributes:nil error:nil];

            [fileManager createFileAtPath:[DocumentsPath stringByAppendingString:@"/image.png"] contents:data attributes:nil];

            

            //得到选择后沙盒中图片的完整路径

            NSString* filePath = [[NSString alloc]initWithFormat:@"%@%@",DocumentsPath,  @"/image.png"];

            LYLog(@"file path :%@", filePath);

             

            //关闭相册界面

            [picker dismissViewControllerAnimated:YES completion:nil];

     

    //        self.myImageView.image = image;

     

            

        }

    }

     

     

    如果还有不了解的,可以参考 http://www.cocoachina.com/ios/20151207/14376.html 。

  • 相关阅读:
    linux-01-04(创建文件夹mkdir,进入目录命令cd,创建文件命令 echo cp vim touch等,批量创建文件操作)
    linux-05(tar命令的使用)
    linux-06(移动命令mv)
    linux-07(复制命令cp)
    linux-08(查看命令历史记录history)
    cookie
    vue-router路由懒加载
    setTimeout async promise执行顺序总结
    forEach陷阱
    函数节流与函数防抖之间的区别
  • 原文地址:https://www.cnblogs.com/loying/p/5079728.html
Copyright © 2011-2022 走看看