图像处理——创建和使用UIImage对象

使用图像

UIImage、CGImage、CIImage三个图像类,之间可以相互转化,不过ARC只对Objective-C对象起作用,所以转化会有内存泄漏风险。

创建图像

UIImage有很多的创建方法,可以根据图像来源的不同进行分类。iOS设备中图像来源主要有4种不同渠道:

  • 从应用程序包中加载;
  • 从应用程序沙箱目录加载;
  • 从云服务器端获取;
  • 从设备图片库选取或从照相机抓取。

一个简单的例子展示:

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imageView;
@property (weak, nonatomic) IBOutlet UIButton *loadBundle;

- (IBAction)loadBundle:(id)sender;
- (IBAction)loadSandbox:(id)sender;
- (IBAction)loadWebService:(id)sender;
/**
 复制图片到沙箱目录下
 */
-(void)p_createEditableCopyOfDatabaseIfNeeded;
/**
 获取沙盒存储路径
 */
-(NSString *)p_applicationDocumentsDirectoryFile;

@end

static const NSString *kFileName = @"flower.jpg";

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _imageView.image = [UIImage imageNamed:@"air.jpg"];
}

/**
 复制图片到沙箱目录下
 */
-(void)p_createEditableCopyOfDatabaseIfNeeded
{
    NSFileManager *fileManager = [NSFileManager defaultManager];
    NSString *writableDBPath = [self p_applicationDocumentsDirectoryFile];
    
    BOOL dbexits = [fileManager fileExistsAtPath:writableDBPath];
    if (!dbexits)
    {
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:kFileName];
        NSError *error;
        BOOL success = [fileManager copyItemAtPath:defaultDBPath
                                            toPath:writableDBPath
                                             error:&error];
        if (!success)
        {
            NSAssert1(0, @"错误写入文件:‘%@’。", [error localizedDescription]);
        }
    }
}

/**
 获取沙盒存储路径
 */
-(NSString *)p_applicationDocumentsDirectoryFile
{
    NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
    NSString *path = [documentDirectory stringByAppendingPathComponent:kFileName];
    return path;
}

- (IBAction)loadWebService:(id)sender
{
    NSURL *url = [NSURL URLWithString:@"https://ns-strategy.cdn.bcebos.com/ns-strategy/upload/fc_big_pic/part-00542-3808.jpg"];
    NSData *data = [[NSData alloc] initWithContentsOfURL:url];
    UIImage *image = [[UIImage alloc] initWithData:data];
    _imageView.image = image;
}

- (IBAction)loadSandbox:(id)sender
{
    //复制图片到沙箱目录下
    [self p_createEditableCopyOfDatabaseIfNeeded];
    
    NSString *path = [self p_applicationDocumentsDirectoryFile];
    UIImage *image = [UIImage imageWithContentsOfFile:path];
    _imageView.image = image;
}

- (IBAction)loadBundle:(id)sender
{
    NSString *path = [[NSBundle mainBundle] pathForResource:kFileName ofType:@""];
    UIImage *img = [UIImage imageWithContentsOfFile:path];
    _imageView.image = img;
}

@end

从设备图片库选取或从照相机抓取

相薄是可以查看所有图片,而相机胶卷是通过照相机拍摄或截屏获得的图片。

UIImagePickerController的主要属性是sourceType,它是一个枚举值:

  • UIImagePickerControllerSourceTypePhotoLibrary,图片来源于“相薄”;
  • UIImagePickerControllerSourceTypeCamera,图片来源于“照相机”;
  • UIImagePickerControllerSourceTypeSavedPhotosAlbum,图片来源于“相机胶卷”。

一个例子代码:

- (IBAction)p_pickPhotoLibrary:(id)sender
{
    if (_imagePicker == nil)
    {
        _imagePicker = [[UIImagePickerController alloc] init];
    }
    _imagePicker.delegate = self;
    _imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    
    [self presentViewController:_imagePicker animated:YES completion:nil];
}

- (IBAction)p_pickPhotoCamera:(id)sender
{
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        _imagePicker = [[UIImagePickerController alloc] init];
        _imagePicker.delegate = self;
        _imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        [self presentViewController:_imagePicker animated:YES completion:^{
            
        }];
    }
    else
    {
        NSLog(@"照相机不可用");
    }
}

-(void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info
{
    UIImage *originalImage = info[UIImagePickerControllerOriginalImage];
    _imageView.image = originalImage;
    _imageView.contentMode = UIViewContentModeScaleAspectFill;

    [self dismissViewControllerAnimated:YES completion:nil];
}

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
    [self dismissViewControllerAnimated:YES completion:nil];
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Win_77

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值