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];
}
复制代码

 

 
复制代码

posted on   ACM_Someone like you  阅读(621)  评论(0编辑  收藏  举报

编辑推荐:
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
阅读排行:
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
· 上周热点回顾(2.17-2.23)

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示