【iOS开发系列】更换头像(相机、相册)

来源:互联网 发布:批量修改文件名称软件 编辑:程序博客网 时间:2024/06/05 17:49
/** *  更换头像按钮点击 */- (void)userIconChange{    UIActionSheet *myActionSheet = [[UIActionSheet alloc]                                    initWithTitle:nil                                    delegate:self                                    cancelButtonTitle:@"取消"                                    destructiveButtonTitle:nil                                    otherButtonTitles: @"从相册选择", @"拍照",nil];    [myActionSheet showInView:self.view];}

/** *  警告框 */-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{    switch (buttonIndex)    {            /* 从相册中选取照片 */        case 0:            [self LocalPhoto];            break;            /* 拍照 */        case 1:            [self takePhoto];            break;        default:            break;    }}

/** *  从相册选择照片 */-(void)LocalPhoto{    UIImagePickerController *picker = [[UIImagePickerController alloc] init];        /* 资源类型为图片库 */    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;    picker.delegate = self;        /* 设置选择后的图片可被编辑 */    picker.allowsEditing = YES;    [self presentModalViewController:picker animated:YES];}

/** *  拍照 */-(void)takePhoto{    /* 资源类型为照相机 */    UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;        UIImagePickerControllerQualityType qualityType = UIImagePickerControllerQualityTypeHigh;    /* 判断是否有相机 */    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera])    {        UIImagePickerController *picker = [[UIImagePickerController alloc] init];        picker.delegate = self;                /* 设置拍照后的图片可被编辑 */        picker.allowsEditing = YES;                /* 资源类型为照相机 */        picker.sourceType = sourceType;                /* 品质为高 */        picker.videoQuality = qualityType;        [self presentModalViewController:picker animated:YES];    }    else    {        NSLog(@"该设备无摄像头");    }}

/** *  图像选取器的委托方法,选完图片后回调该方法 */-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{        /* 当图片不为空时显示图片并保存图片 */    if (image != nil)    {        /* 图片显示在界面上 */        // [_iconButton setBackgroundImage:image forState:UIControlStateNormal]; // 显示在按钮上        [_iconButton setImage:image forState:UIControlStateNormal];                /* 以下是保存文件到沙盒路径下 */        /* 把图片转成NSData类型的数据来保存文件 */        NSData *data;                /* 判断图片是不是png格式的文件 */        if (UIImagePNGRepresentation(image))        {            /* 返回为png图像 */            data = UIImagePNGRepresentation(image);        }        else        {            /* 返回为JPEG图像 */            data = UIImageJPEGRepresentation(image, 1.0);        }                /* 保存 */        [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];    }    /* 关闭相册界面 */    [picker dismissModalViewControllerAnimated:YES];}

当然、如果是网络应用需要将更换的头像上传到服务器 (留在下一步完成........)

0 0
原创粉丝点击