拍照或者從相冊選擇

import Foundation
import UIKit

class SellAddView : UIViewController, UIImagePickerControllerDelegate, UIActionSheetDelegate, UINavigationControllerDelegate {
    //打開相機
    @IBAction func doCamera() {
        var sheet:UIActionSheet;
        if(UIImagePickerController.isSourceTypeAvailable(UIImagePickerControllerSourceType.Camera)){
            sheet = UIActionSheet(title: nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "從相冊選擇","拍照");
        } else {
            sheet = UIActionSheet(title:nil, delegate: self, cancelButtonTitle: "取消", destructiveButtonTitle: nil, otherButtonTitles: "從相冊選擇")
        }
        sheet.showInView(self.view)
    }

    func actionSheet(actionSheet: UIActionSheet, clickedButtonAtIndex buttonIndex: Int) {
        var sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
        if(buttonIndex != 0){
            if(buttonIndex==1){ //相冊
                sourceType = UIImagePickerControllerSourceType.PhotoLibrary;
            }else{
                sourceType = UIImagePickerControllerSourceType.Camera;
            }
            let imagePickerController = UIImagePickerController();
            imagePickerController.delegate = self;
            imagePickerController.allowsEditing = true; //true爲拍照、選擇完進入圖片編輯模式
            imagePickerController.sourceType = sourceType;
            if #available(iOS 8.0, *) {
                imagePickerController.modalPresentationStyle = UIModalPresentationStyle.FullScreen;
            };
            self.presentViewController(imagePickerController, animated: true, completion: { () -> Void in

            })
        }
    }

    func imagePickerControllerDidCancel(picker:UIImagePickerController)    {
        self.dismissViewControllerAnimated(true, completion: nil)
    }

    func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
        var imageToSave:UIImage;
        //得到編輯完的圖片
        imageToSave = info[UIImagePickerControllerEditedImage] as! UIImage;

        var ratio:CGFloat = 1.0;//壓縮比
        var imageData = UIImageJPEGRepresentation(imageToSave, ratio);
        var ratioCnt = 0;
        while(imageData!.length > (1024*200)) { //不大於200KB
            ratioCnt++;
            if(ratioCnt > 5) {
                break;
            }
            if(imageData!.length > (1024*2000)) {
                ratio = ratio*0.1;
            } else if(imageData!.length > (1024*1000)) {
                ratio = ratio*0.5;
            } else {
                ratio = ratio*0.9;
            }
            imageData = UIImageJPEGRepresentation(imageToSave, ratio);
        }

        //取得編輯完的圖片
        let image = UIImage(data: imageData!);
        if(image != nil) {
            self.imageView.image = image;
            self.camera.hidden = true;
            self.imageView.hidden = false;
        }
        dismissViewControllerAnimated(true, completion: nil);

    }

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章