技术文章分类(180)

技术随笔(11)

IOS UIImagePickerController的使用(IOS拍照)

 

1,实现UIImagePickerControllerDelegate, UINavigationControllerDelegate这两个代理

2,这里是拍照的触发事件

/*
        拍照
    */
    func takePhoto(sender:AnyObject!){
        
        var imageController:UIImagePickerController = UIImagePickerController()
        imageController.sourceType = .Camera
        imageController.videoQuality = .TypeHigh
        imageController.videoMaximumDuration = 10.0
        imageController.allowsEditing = true
        imageController.delegate = self
        self.presentViewController(imageController,animated:true,completion:nil) //貌似说只能以模态形式展示
    }

3,

  func imagePickerController(picker: UIImagePickerController!, didFinishPickingMediaWithInfo info: NSDictionary!){
        var image:UIImage!
        if picker.allowsEditing {
            image = info.objectForKey(UIImagePickerControllerEditedImage) as UIImage
        }else{
            image = info.objectForKey(UIImagePickerControllerOriginalImage) as UIImage
        }
        
        UIImageWriteToSavedPhotosAlbum(image,self,Selector("imageDidFinishSavingWithErrorContextInfo:error:contextInfo:"),nil) //这是存到了手机图库(如果不需要存到图库,这句可不要,对应的Selector也不要。如果要,那么对应Selector方法的名字是固定的)
        let storeFilePath:NSArray = NSSearchPathForDirectoriesInDomains(.DocumentDirectory,.UserDomainMask,true)
        var doucumentsDirectiory:String = storeFilePath.objectAtIndex(0) as String
        var path:String = doucumentsDirectiory.stringByAppendingPathComponent(self.Li_detailTimeString())
        var result:Bool = UIImagePNGRepresentation(image).writeToFile(path, atomically:true)  //这是存到了沙盒
        if result == false {
            println("Li_storageImgToDomain照片存入沙盒出错")
        }
        println(path)
        self.dismissModalViewControllerAnimated(true)
    }
    func imagePickerControllerDidCancel(picker: UIImagePickerController!){
        self.dismissModalViewControllerAnimated(true)
    }
    //照片的命名规则
    func Li_detailTimeString()->String{
        var date:NSDate = NSDate()
        var dateFormatter:NSDateFormatter = NSDateFormatter()
        dateFormatter.dateFormat = "yyyyMMddhhmmss"
        var dateString:String = dateFormatter.stringFromDate(date)
        return dateString
    }
  //存到图库才需要调用,否则一般不用
func imageDidFinishSavingWithErrorContextInfo(image:UIImage!,error:NSErrorPointer,contextInfo:CMutableVoidPointer){ if error != nil { println("saved to album successed") }else{ println(error) } }

完毕

 

拓展:

从图库选择照片,只需要再写一个触发方法,其他代理方法都可以和拍照的代理共用

    //从相册中选择照片
    func selectPhotoFromAlbum(){
        var imageController:UIImagePickerController = UIImagePickerController()
        imageController.sourceType = .PhotoLibrary
        imageController.videoMaximumDuration = 10.0
        imageController.allowsEditing = true
        imageController.delegate = self
        self.presentViewController(imageController,animated:true,completion:nil) //貌似说只能以模态形式展示
        println("从相册中选择照片")
    }

 

 

ios拍照object-c版
github 下载demo:https://github.com/MartinLi841538513/MartinDemos (一切以demo为准)

这个简单多了,原理一样

#import "TakePhotoViewControllerViewController.h"

@interface TakePhotoViewControllerViewController ()<UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
@end
@implementation TakePhotoViewControllerViewController

- (IBAction)takePhotoAction:(id)sender {
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"更换头像" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"相册", nil];
    [action showInView:self.view.window];
}

#pragma UIActionSheetDelegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [self showImagePickerControllerWithSourceType:UIImagePickerControllerSourceTypeCamera];
    }else if(buttonIndex==1){
        [self showImagePickerControllerWithSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
    }
}

#pragma UIImagePickerControllerDelegate
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *image = nil;
    if (picker.allowsEditing) {
        image = info[UIImagePickerControllerEditedImage];
    }else{
        image = info[UIImagePickerControllerOriginalImage];
    }
    [picker dismissViewControllerAnimated:YES completion:^(void){
        [self.photoButton setTitle:@"" forState:UIControlStateNormal];
        [self.photoButton setImage:image forState:UIControlStateNormal];
    }];
}

-(void)showImagePickerControllerWithSourceType:(UIImagePickerControllerSourceType)sourceType{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.sourceType = sourceType;
    picker.allowsEditing = YES;
    picker.delegate = self;
    [self presentViewController:picker animated:YES completion:nil];
}
@end

这里我是对button setImage和touch事件的,而button setImage之前需要做一个处理,否则会出现设置失败。set button type为custom。

 

posted @ 2014-07-10 14:35  坤哥MartinLi  阅读(1370)  评论(0编辑  收藏  举报