zoukankan      html  css  js  c++  java
  • IOS 从系统图库中获取 图片 并设置为头像

    总会遇到一些应用 要拍照 设置图片之类的的  总结一个方法备用  以后 会 继续拓展到 多图

    @interface AddCustomerViewController : UIViewController<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
    {
        UIImage *chosenImage;//做显示在self.view 上的image
        NSString *tempImagePath;//图片路径
        UIButton *button_Photo;//点击从系统选图片
    }
    @end
    //以上代理 和参数 都是必须有的
    
    
    -(void)TakePhoto
    {
        UIActionSheet *actionSheet =[[UIActionSheet alloc]initWithTitle:@"您想如何获取照片?" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照", nil];
       [actionSheet showInView:self.view];
    }
    -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
    {
        /*
        NSLog(@"%ld",(long)buttonIndex);//2--->取消
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.allowsEditing = YES;
        if(buttonIndex==0)
        {//拍照
            picker.sourceType=UIImagePickerControllerSourceTypeCamera;
        }
        else if(buttonIndex==1)
        {//图库
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        }
        [self presentViewController:picker animated:YES completion:NULL];
         */
        NSLog(@"%ld",(long)buttonIndex);//2--->取消
       
        if(buttonIndex==0)
        {//拍照
            UIImagePickerController *picker = [[UIImagePickerController alloc] init];
            picker.delegate = self;
            picker.allowsEditing = YES;
            picker.sourceType=UIImagePickerControllerSourceTypeCamera;
            [self presentViewController:picker animated:YES completion:NULL];
        }
        
        
    }
    
    #pragma -mark UIImagePickerController delegate
    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        chosenImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        //删除缓存文件
        if ([[NSFileManager defaultManager] fileExistsAtPath:tempImagePath]) {
            NSLog(@"the image is exist");
            NSFileManager *defaultManager;
            
            defaultManager = [NSFileManager defaultManager];
            NSError *error = [[NSError alloc] init];
            [defaultManager removeItemAtPath:tempImagePath error:&error];
            
        }
        [UIImagePNGRepresentation(chosenImage) writeToFile:[ NSTemporaryDirectory() stringByAppendingPathComponent:@"temp_image.png"] atomically:YES];
        
        BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:tempImagePath];
        
        if (fileExists) {
            NSLog(@"success");
            customer_Inform.image_Head=chosenImage;
            [button_Photo setImage:chosenImage forState:UIControlStateNormal];
            
        }
        [picker dismissViewControllerAnimated:YES completion:NULL];
        
        if (fileExists) {
            NSLog(@"success");
            [SGInfoAlert showInfo:@" 照片添加成功! "
                          bgColor:[[UIColor darkGrayColor] CGColor]
                           inView:self.view
                         vertical:0.5];
        }
        else
        {
            [SGInfoAlert showInfo:@" 照片添加不成功! "
                          bgColor:[[UIColor darkGrayColor] CGColor]
                           inView:self.view
                         vertical:0.5];
        }
    //    //也能用
    //    [button_Photo setImage:[info objectForKey:@"UIImagePickerControllerOriginalImage"] forState:UIControlStateNormal];
    //    [picker dismissViewControllerAnimated:YES completion:NULL];
    }
    
    
    
     
  • 相关阅读:
    反射
    注解
    file
    exception(异常)
    MySQL问题
    maven 中 遇到的问题
    Java读取文本数字
    人民币-欧元预测(ARIMA算法)代码
    云平台项目--学习经验--jsrender前端渲染模板
    云平台项目--学习经验--BootstrapValidate表单验证插件
  • 原文地址:https://www.cnblogs.com/someonelikeyou/p/3667056.html
Copyright © 2011-2022 走看看