提到從攝像頭/相冊(cè)獲取圖片是面向終端用戶的,由用戶去瀏覽并選擇圖片為程序使用磅崭。在這里柱徙,我們需要過UIImagePickerController類來和用戶交互。
使用UIImagePickerController和用戶交互应闯,我們需要實(shí)現(xiàn)2個(gè)協(xié)議<UIImagePickerControllerDelegate,UINavigationControllerDelegate>。
View Code
代碼如下 復(fù)制代碼
pragma mark 從用戶相冊(cè)獲取活動(dòng)圖片
- (void)pickImageFromAlbum
{ imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate =self; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker.allowsEditing =YES;
[self presentModalViewController:imagePicker animated:YES];
}
我們來看看上面的從相冊(cè)獲取圖片挂捻,我們首先要實(shí)例化UIImagePickerController對(duì)象碉纺,然后設(shè)置imagePicker對(duì)象為當(dāng)前對(duì)象,設(shè)置imagePicker的圖片來源為UIImagePickerControllerSourceTypePhotoLibrary细层,表明當(dāng)前圖片的來源為相冊(cè)惜辑,除此之外還可以設(shè)置用戶對(duì)圖片是否可編輯唬涧。
View Code
代碼如下 復(fù)制代碼
pragma mark 從攝像頭獲取活動(dòng)圖片
- (void)pickImageFromCamera
{
imagePicker = [[UIImagePickerController alloc] init]; imagePicker.delegate =self; imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera; imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical; imagePicker.allowsEditing =YES;
[self presentModalViewController:imagePicker animated:YES];
}
//打開相機(jī)
-
(IBAction)touch_photo:(id)sender {
// for iphone
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];}
pickerImage.delegate =self;
pickerImage.allowsEditing =YES;//自定義照片樣式
[self presentViewController:pickerImage animated:YES completion:nil];
}
以上是從攝像頭獲取圖片疫赎,和從相冊(cè)獲取圖片只是圖片來源的設(shè)置不一樣,攝像頭圖片的來源為UIImagePickerControllerSourceTypeCamera碎节。
在和用戶交互之后捧搞,用戶選擇好圖片后,會(huì)回調(diào)選擇結(jié)束的方法狮荔。
-(void)imagePickerController:(UIImagePickerController)picker didFinishPickingMediaWithInfo:(NSDictionary)info
{
//初始化imageNew為從相機(jī)中獲得的--
UIImage imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//設(shè)置image的尺寸
CGSize imagesize = imageNew.size;
imagesize.height =626;
imagesize.width =413;
//對(duì)圖片大小進(jìn)行壓縮--
imageNew = [self imageWithImage:imageNew scaledToSize:imagesize];
NSData imageData = UIImageJPEGRepresentation(imageNew,0.00001);
if(m_selectImage==nil)
{
m_selectImage = [UIImage imageWithData:imageData];
NSLog(@"m_selectImage:%@",m_selectImage);
[self.TakePhotoBtn setImage:m_selectImage forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
return ;
}
[picker release];
}
//對(duì)圖片尺寸進(jìn)行壓縮--
-(UIImage)imageWithImage:(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;
}
圖片保存到本地document里面--以及圖片格式的轉(zhuǎn)換
IOS開發(fā)之保存圖片到Documents目錄及PNG胎撇,JPEG格式相互轉(zhuǎn)換 -
(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [NSString stringWithString:[self getPath:@"image1"]]; //將圖片存儲(chǔ)到本地documents [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:dataattributes:nil]; UIImage *editedImage = [[UIImage alloc] init]; editedImage = image; CGRect rect = CGRectMake(0, 0, 64, 96); UIGraphicsBeginImageContext(rect.size); [editedImage drawInRect:rect]; editedImage = UIGraphicsGetImageFromCurrentImageContext(); UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom]; imageButton.frame = CGRectMake(10, 10, 64, 96); [imageButton setImage:editedImage forState:UIControlStateNormal]; [self.view addSubview:imageButton]; [imageButton addTarget:self action:@selector(imageAction:)forControlEvents:UIControlEventTouchUpInside]; [ipc dismissModalViewControllerAnimated:YES];
} else {
NSLog(@"MEdia");
}
上面的代碼是當(dāng)從相冊(cè)里面選取圖片之后保存到本地程序沙盒,在上面我們得到的圖片中不能夠得到圖片名字殖氏,
以及不清楚圖片格式晚树,所以這個(gè)時(shí)候我們需要將其轉(zhuǎn)換成NSdata二進(jìn)制存儲(chǔ),
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}
UIImagePNGRepresentation轉(zhuǎn)換PNG格式的圖片為二進(jìn)制雅采,如果圖片的格式為JPEG則返回nil爵憎;
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:data attributes:nil]; 將圖片保存為PNG格式
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil]; 將圖片保存為JPEG格式
我們也可以寫成下面的格式存儲(chǔ)圖片
NSString *pngImage = [filePath stringByAppendingPathComponent:@"Documents/image.png"];
NSString *jpgImage = [filePath stringByAppendingPathComponent:@"Documents/image.jpg"];
[data writeToFile:pngImage atomically:YES];
[data writeToFile:jpgImage atomically:YES];提到從攝像頭/相冊(cè)獲取圖片是面向終端用戶的慨亲,由用戶去瀏覽并選擇圖片為程序使用。在這里宝鼓,我們需要過UIImagePickerController類來和用戶交互刑棵。
使用UIImagePickerController和用戶交互,我們需要實(shí)現(xiàn)2個(gè)協(xié)議<UIImagePickerControllerDelegate,UINavigationControllerDelegate>愚铡。
View Code
代碼如下 復(fù)制代碼
pragma mark 從用戶相冊(cè)獲取活動(dòng)圖片
-
(void)pickImageFromAlbum
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate =self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing =YES;[self presentModalViewController:imagePicker animated:YES];
}
我們來看看上面的從相冊(cè)獲取圖片蛉签,我們首先要實(shí)例化UIImagePickerController對(duì)象,然后設(shè)置imagePicker對(duì)象為當(dāng)前對(duì)象沥寥,設(shè)置imagePicker的圖片來源為UIImagePickerControllerSourceTypePhotoLibrary碍舍,表明當(dāng)前圖片的來源為相冊(cè),除此之外還可以設(shè)置用戶對(duì)圖片是否可編輯邑雅。
View Code
代碼如下 復(fù)制代碼
pragma mark 從攝像頭獲取活動(dòng)圖片
-
(void)pickImageFromCamera
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate =self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing =YES;[self presentModalViewController:imagePicker animated:YES];
}
//打開相機(jī) -
(IBAction)touch_photo:(id)sender {
// for iphone
UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
pickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];}
pickerImage.delegate =self;
pickerImage.allowsEditing =YES;//自定義照片樣式
[self presentViewController:pickerImage animated:YES completion:nil];
}
以上是從攝像頭獲取圖片乒验,和從相冊(cè)獲取圖片只是圖片來源的設(shè)置不一樣,攝像頭圖片的來源為UIImagePickerControllerSourceTypeCamera蒂阱。
在和用戶交互之后锻全,用戶選擇好圖片后,會(huì)回調(diào)選擇結(jié)束的方法录煤。
-(void)imagePickerController:(UIImagePickerController)picker didFinishPickingMediaWithInfo:(NSDictionary)info
{
//初始化imageNew為從相機(jī)中獲得的--
UIImage imageNew = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
//設(shè)置image的尺寸
CGSize imagesize = imageNew.size;
imagesize.height =626;
imagesize.width =413;
//對(duì)圖片大小進(jìn)行壓縮--
imageNew = [self imageWithImage:imageNew scaledToSize:imagesize];
NSData imageData = UIImageJPEGRepresentation(imageNew,0.00001);
if(m_selectImage==nil)
{
m_selectImage = [UIImage imageWithData:imageData];
NSLog(@"m_selectImage:%@",m_selectImage);
[self.TakePhotoBtn setImage:m_selectImage forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
return ;
}
[picker release];
}
//對(duì)圖片尺寸進(jìn)行壓縮--
-(UIImage)imageWithImage:(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;
}
圖片保存到本地document里面--以及圖片格式的轉(zhuǎn)換
IOS開發(fā)之保存圖片到Documents目錄及PNG鳄厌,JPEG格式相互轉(zhuǎn)換 -
(void)imagePickerController:(UIImagePickerController )picker didFinishPickingMediaWithInfo:(NSDictionary)info {
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]){
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *filePath = [NSString stringWithString:[self getPath:@"image1"]]; //將圖片存儲(chǔ)到本地documents [fileManager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil]; [fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:dataattributes:nil]; UIImage *editedImage = [[UIImage alloc] init]; editedImage = image; CGRect rect = CGRectMake(0, 0, 64, 96); UIGraphicsBeginImageContext(rect.size); [editedImage drawInRect:rect]; editedImage = UIGraphicsGetImageFromCurrentImageContext(); UIButton *imageButton = [UIButton buttonWithType:UIButtonTypeCustom]; imageButton.frame = CGRectMake(10, 10, 64, 96); [imageButton setImage:editedImage forState:UIControlStateNormal]; [self.view addSubview:imageButton]; [imageButton addTarget:self action:@selector(imageAction:)forControlEvents:UIControlEventTouchUpInside]; [ipc dismissModalViewControllerAnimated:YES];
} else {
NSLog(@"MEdia");
}
上面的代碼是當(dāng)從相冊(cè)里面選取圖片之后保存到本地程序沙盒,在上面我們得到的圖片中不能夠得到圖片名字妈踊,
以及不清楚圖片格式了嚎,所以這個(gè)時(shí)候我們需要將其轉(zhuǎn)換成NSdata二進(jìn)制存儲(chǔ),
image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *data;
if (UIImagePNGRepresentation(image) == nil) {
data = UIImageJPEGRepresentation(image, 1);
} else {
data = UIImagePNGRepresentation(image);
}
UIImagePNGRepresentation轉(zhuǎn)換PNG格式的圖片為二進(jìn)制廊营,如果圖片的格式為JPEG則返回nil歪泳;
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.png"] contents:data attributes:nil]; 將圖片保存為PNG格式
[fileManager createFileAtPath:[filePath stringByAppendingString:@"/image.jpg"] contents:data attributes:nil]; 將圖片保存為JPEG格式
我們也可以寫成下面的格式存儲(chǔ)圖片
NSString *pngImage = [filePath stringByAppendingPathComponent:@"Documents/image.png"];
NSString *jpgImage = [filePath stringByAppendingPathComponent:@"Documents/image.jpg"];
[data writeToFile:pngImage atomically:YES];
[data writeToFile:jpgImage atomically:YES];