iOS新手入门常见问题4-从相册选择图片/摄像头拍照选图

2016年07月08日 iOS 暂无评论 阅读699次


iOS新手入门问题4-从相册选择图片/摄像头拍照选图

和Android设备类似,iOS设备选择图片也分为从图库(相册)选择,或调用摄像头拍照。

为实现该功能,iOS为我们提供的接口:就是了不起的UIImagePickerController。

使用UIImagePickerController来选择图片,主要完成下面几步的操作:

  1. h文件声明当前类要实现的协议:UIImagePickerControllerDelegate

  2. 初始化UIImagePickerController类

  3. 为UIImagePickerController类的实例设置数据来源

  4. 设置设置代理

  5. 其他辅助设置

iOS 从相册选取图片

- (void)pickImageFromAlbum {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    // 当前图片的来源为相册
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing = YES;
    [self presentModalViewController:imagePicker animated:YES];
} 

iOS 通过摄像头拍照得到图片

- (void)pickImageFromCamera {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    // 当前图片的来源为摄像头
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
    imagePicker.allowsEditing = YES;
    [self presentModalViewController:imagePicker animated:YES];
} 

得到图片后的回调函数(要实现的代理方法)

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
    UIImage *theImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(120.0, 120.0)];
    UIImage *midImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
    UIImage *bigImage = [self imageWithImageSimple:image scaledToSize:CGSizeMake(440.0, 440.0)];
    [self saveImage:theImage WithName:@"salesImageSmall.jpg"];
    [self saveImage:midImage WithName:@"salesImageMid.jpg"];
    [self saveImage:bigImage WithName:@"salesImageBig.jpg"];
    [self dismissModalViewControllerAnimated:YES];
    // use the image to do something
} 

用户取消选择图片时的回调函数(要实现的另一个代理方法)

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
    // do sth
} 

iOS压缩图片到目标尺寸

- (UIImage*)imageWithImageSimple:(UIImage *)image scaledToSize:(CGSize)newSize {
    // Create a graphics image context
    UIGraphicsBeginImageContext(newSize);
    // Tell the old image to draw in this new context, with the desired
    // new size
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Get the new image from the context
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    // End the context
    UIGraphicsEndImageContext();
    // Return the new image.
    return newImage;
} 

iOS保存图片到沙盒(Documents目录下)

获得这几种尺寸的图像后,我们一般先要放到UI中的ImageView中,其次还想要存储起来。存储可以到沙盒或网络(上传),这里先不涉及网络上传图片的相关内容,只看下将图片存储到应用程序沙盒(Documents)下。

#pragma mark 保存图片到沙盒 Documents
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName {
    NSData* imageData = UIImagePNGRepresentation(tempImage);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentsPath = [paths objectAtIndex:0];
    NSString* fullPath = [documentsPath stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPath atomically:NO];
} 

iOS从沙盒(Documents目录)读取图片

保存到沙盒下之后,还要有从Documents目录下读取图片的方法。

#pragma mark 从文档目录下获取Documents路径
- (UIImage *)getImageFromDocumentsWithImageName:(NSString *)imgName {
    NSString *documetsPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
    NSString *fullPath = [documetsPath stringByAppendingPathComponent:imgName];
    return [UIImage imageNamed:fullPath];
} 

另外,除了刚才代理要实现的方法之外,还有另外一个用来当用户取消选择时调用的。

分享本文至:

WRITTEN BY

avatar
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

欢迎留言




用户登录

sitemap