#import "ViewController.h"
#import "GTMBase64.h"
#import <AssetsLibrary/AssetsLibrary.h>
@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@property (nonatomic,strong) UIViewController * parentView;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)photosOrcamrae:(id)sender {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"选择" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *deleteAction){
[self takePictureClick];
}];
UIAlertAction *archiveAction = [UIAlertAction actionWithTitle:@"相册中选取" style:UIAlertActionStyleDefault handler:^(UIAlertAction *archiveAction){
[self pictureBtnClick];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *cancelAction){
}];
[alertController addAction:cancelAction];
[alertController addAction:deleteAction];
[alertController addAction:archiveAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//调用相机
- (void)takePictureClick{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController *pic = [[UIImagePickerController alloc] init];
pic.sourceType = UIImagePickerControllerSourceTypeCamera;
pic.allowsEditing = YES;
pic.showsCameraControls = YES;
pic.delegate =self;
[self presentViewController:pic animated:YES completion:nil];
}
}
- (void)image:(UIImage *)image didFinishSavingWithError:
(NSError *)error contextInfo:(void *)contextInfo;
{
// Handle the end of the image write process
if (!error){
NSLog(@"Image written to photo album");
}else{
NSLog(@"Error writing to photo album: %@",
[error localizedDescription]);
}
}
//相册选取图片
- (void)pictureBtnClick{
UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
ipc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
ipc.allowsEditing = YES;
ipc.delegate =self;
[self presentViewController:ipc animated:YES completion:nil];
}
#pragma mark - ImagePickerDelegate
-(UIImage*)imageWithImage:(UIImage*)image scaledToSize:(CGSize)newSize
{
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
return newImage;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//相册和相机拍摄的照片选择都走这个代理
UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
CGSize imagesize = image.size;
imagesize.height =200;
imagesize.width =200;
UIImage *img = [self imageWithImage:image scaledToSize:imagesize];
[self updateUserHeaderWithImage:img];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)updateUserHeaderWithImage:(UIImage *)image {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
NSString *imageStr = [GTMBase64 stringByEncodingData:imageData];
//相机拍摄照片存储照片
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
[library writeImageToSavedPhotosAlbum:[image CGImage] orientation:(ALAssetOrientation)[image imageOrientation] completionBlock:^(NSURL *assetURL, NSError *error){
if (error) {
// error handling
} else {
// success handling
}
}];
}
//这里是选择的照片 进行处理
}
@end
个人手写,不喜勿喷,下次发布仿写相册多张选取和相机拍照以后的多张选取
干货才是硬道理
本文介绍如何在iOS应用中集成相册选取和相机拍摄功能,包括使用UIImagePickerController选择图片,调整图片尺寸,并将拍摄的照片保存到相册。
1万+

被折叠的 条评论
为什么被折叠?



