照相功能

調用相機而且上傳照片的方法

#import "ViewController.h"
#import "AFNetworking.h"
@interface ViewController () <UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
 
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
 
@end
 
@implementation ViewController
 
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}
 
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"請選擇照片" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相機",@"相冊", nil];
    /**
     *  window表明着最高處,全部能看到的view,後面都是window。
     *  當push相機控制器的時候,self.view就自動移除了。而當dismiss控制器的時候,由於self.view移除了,全部sheet沒法寄居在view的上面,而固定在self.view.window,就能夠保證,sheet必定在view視圖上
     */
    [sheet showInView:self.view.window];
     
}
 
-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
     
    ipc.delegate = self;
    ipc.allowsEditing = YES;  //相片是否可編輯 
    switch (buttonIndex) {
        case 0:
            if (![UIImagePickerController isSourceTypeAvailable:
                  UIImagePickerControllerSourceTypeCamera]) return;
                ipc.sourceType = UIImagePickerControllerSourceTypeCamera;
            break;
        case 1:
            if (![UIImagePickerController isSourceTypeAvailable:
                  UIImagePickerControllerSourceTypePhotoLibrary]) return;
                ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            break;
        
        default:
            return;
            break;
    }
    [self presentViewController:ipc animated:YES completion:nil];
}
 
/**
 *  選擇完照片後調用的方法
 *
 *
 *  @param info   全部相片的信息都在這個字典
 */
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    [self dismissViewControllerAnimated:YES completion:nil];
     
    //從字典key獲取image的地址
    UIImage *image = info[UIImagePickerControllerOriginalImage];
     
    self.imageView.image = image;
     
     
}
 
- (IBAction)upload:(UIButton *)sender {
     
    AFHTTPRequestOperationManager *mrg = [[AFHTTPRequestOperationManager alloc] init];
     
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = @"123";
 
     
     NSString *url = @"http://192.168.130.110:8080/MJServer/upload";
     
    /**
     *  上傳時候調用的方法
     *
     *  params   post的參數,字典
     *  formData formData是post上去的數據
     *  name     服務器接受文件的名字
     *  fileName 文件的名字
     */
    [mrg POST:url parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
        
        NSData *data = UIImageJPEGRepresentation(self.imageView.image, 1.0);
        [formData appendPartWithFileData:data name:@"file" fileName:@"1.jpg" mimeType:@"image/jpeg"];
         
    } success:^(AFHTTPRequestOperation *operation, id responseObject) {
         
        NSLog(@"上傳成功");
         
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"上傳失敗");
    }];
 
}
@end


2 手動存儲相片到相冊

    for (int i = 0; i<=9; i++) {
        UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"00%d.png", i]];
        UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
        NSLog(@"%@", image);
        [NSThread sleepForTimeInterval:1];   //存儲不能夠過快
    }


3 按鈕文字設置成中文

info.plist 中添加Localized resources can be mixed 設置爲YES服務器


4 圖片選擇控制器

當想本身寫一個圖片選擇控制器的時候,須要利用AssetsLibrary.framework,利用這個框架得到全部的相冊圖片
app


5 其餘

switch (buttonIndex) {
        case 0://照相機
        { UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
            [self presentViewController:imagePicker animated:YES completion:nil];
            
        }
            break;
        case 1://攝像機
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
            imagePicker.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
            imagePicker.videoQuality = UIImagePickerControllerQualityTypeLow;
           [self presentViewController:imagePicker animated:YES completion:nil];
            
        }
            break;
        case 2://本地相簿
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeImage, nil];
           [self presentViewController:imagePicker animated:YES completion:nil];
        }
            break;
        case 3://本地視頻
        {
            UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
            imagePicker.delegate = self;
            imagePicker.allowsEditing = YES;
            imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            imagePicker.mediaTypes =  [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
           [self presentViewController:imagePicker animated:YES completion:nil];
            
        }
相關文章
相關標籤/搜索