使用UIactionSheet调用系统相簿并将图片存储到缓存中
2015-06-04 16:45
330 查看
实现UIActionSheetDelegate
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle: @""
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"图库相册",@"拍照", nil];
[menu showInView:self.view];
# pragma mark UIActionSheet method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) {
[self photoalbumr];//图库相册
}else if(buttonIndex==1){
[self photocamera];//拍照
}else if(buttonIndex==2){
}
[actionSheet release];
}
调用系统的相簿
实现UIImagePickerControllerDelegate
-(void)photoalbumr{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing photo library"
message:@"Device does not support a photo library"
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void)photocamera{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagepicker.allowsEditing = YES;
[self presentModalViewController:imagepicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"设备不支持拍照功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
#pragma mark UIImagePicker method
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
/*将图片存储到document下
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
_imgName=@"savedImage.png";
self._imgFilePath=documentsDirectory;
*/
[addPhoto setBackgroundImage:image forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
}
// 完成选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
UIActionSheet *menu = [[UIActionSheet alloc]
initWithTitle: @""
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"图库相册",@"拍照", nil];
[menu showInView:self.view];
# pragma mark UIActionSheet method
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
if (buttonIndex==0) {
[self photoalbumr];//图库相册
}else if(buttonIndex==1){
[self photocamera];//拍照
}else if(buttonIndex==2){
}
[actionSheet release];
}
调用系统的相簿
实现UIImagePickerControllerDelegate
-(void)photoalbumr{
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypePhotoLibrary]) {
UIImagePickerController *picker =
[[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsImageEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentModalViewController:picker animated:YES];
[picker release];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error accessing photo library"
message:@"Device does not support a photo library"
delegate:nil
cancelButtonTitle:@"Drat!"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
-(void)photocamera{
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
UIImagePickerController* imagepicker = [[UIImagePickerController alloc] init];
imagepicker.delegate = self;
imagepicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagepicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagepicker.allowsEditing = YES;
[self presentModalViewController:imagepicker animated:YES];
}
else {
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Sorry"
message:@"设备不支持拍照功能"
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alert show];
[alert release];
}
}
#pragma mark UIImagePicker method
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
/*将图片存储到document下
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:NO];
_imgName=@"savedImage.png";
self._imgFilePath=documentsDirectory;
*/
[addPhoto setBackgroundImage:image forState:UIControlStateNormal];
[picker dismissModalViewControllerAnimated:YES];
}
// 完成选取
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissModalViewControllerAnimated:YES];
}
相关文章推荐
- UIAlertView以及UIAlertController
- 移植 MIUI Framework
- UITableView优化技巧
- 自定义组合控件关于LayoutInflater.from(context).inflate(R.layout.view_title, this,true)的问题
- 【HDU】2818-Building Block(并查集)
- Win7共享问题 映射网盘时出现的错误 the specified server cannot perform the requested operation
- UITextField格式化银行卡号
- 解析duilib 消息处理函数-笔记1
- easyUI 1.3.2 dataGrid动态绑定列名和数据
- 使用Qt Quick设计器
- request,路径问题,转发和重定向
- request和response的知识
- UEditor 1.4.3版本中去掉本地保存功能
- db2 sequences值
- request的用法
- UEditor前端配置项说明
- 百度UEditor修改右下角统计字数包含html样式
- 简单叙说 serialVersionUID 的作用 (用于笔记)
- iOS的UIButton和UILable
- Android酷炫实用的开源框架(UI框架)