1.建立项目。
2.将info.plist文件增加下面内容,不然无法访问:
<key>NSPhotoLibraryUsageDescription</key>
<string>App需要您的同意,才能访问相册</string>
<key>NSAppleMusicUsageDescription</key>
<string>App需要您的同意,才能访问媒体资料库</string>
<key>NSCameraUsageDescription</key>
<string>cameraDesciption</string>
3.类似于换头像,给圆形按钮添加背景图,点击更换头像。图片截取功能yi完善。
//
// ViewController.m
// SheetTest
//
// Created by lishuai on 2017/8/2.
// Copyright © 2017年 lishuai. All rights reserved.
//
#import "ViewController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()<UIImagePickerControllerDelegate, UINavigationControllerDelegate,UIActionSheetDelegate>
@property (nonatomic, strong) UIImageView * imageView;
@property (nonatomic, strong) UIImagePickerController* imagePicker;
@property (nonatomic, strong) UIImageView* HeadShot;
@property (nonatomic, strong) UIButton *Rightbutton;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self button];
//_HeadShot = [[UIImageView alloc]initWithFrame:CGRectMake(200, 200, 160, 160)];
//_HeadShot.frame = CGRectMake(100, 100, 80, 80);
//_HeadShot.contentMode = UIViewContentModeScaleAspectFill;
//[_HeadShot setClipsToBounds:YES];
//[self.view addSubview: _HeadShot];
}
-(void)button {
_Rightbutton = [UIButton buttonWithType:UIButtonTypeCustom] ;
[_Rightbutton setFrame:CGRectMake(100, 100, 100, 100)];
_Rightbutton.layer.cornerRadius = 100/2;
_Rightbutton.layer.borderWidth = 0.5;
_Rightbutton.layer.borderColor = [UIColor whiteColor].CGColor;
_Rightbutton.layer.masksToBounds = YES;
//[Rightbutton setTitle:@"上传图片" forState:UIControlStateNormal];
[_Rightbutton setBackgroundImage:[UIImage imageNamed:@"ic_home_penalty"] forState:UIControlStateNormal];
[_Rightbutton addTarget:self action:@selector(alert:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_Rightbutton];
}
-(void)alert:(UIBarButtonItem *)itemCamera {
_imagePicker = [[UIImagePickerController alloc] init];
_imagePicker.delegate = self;
//显示图片截取框,但是截取后图片功能未写
_imagePicker.allowsEditing=YES;
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:nil
preferredStyle:UIAlertControllerStyleActionSheet];
UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"拍照"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
_imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:_imagePicker animated:YES completion:nil];
}];
UIAlertAction *photoAlbum = [UIAlertAction actionWithTitle:@"从手机相册选择"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
_imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:_imagePicker animated:YES completion:nil];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消"
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
}];
[alertController addAction:cameraAction];
[alertController addAction:photoAlbum];
[alertController addAction:cancelAction];
[self presentViewController:alertController animated:YES completion:nil];
}
//调取选取的图片
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
// UIImage *images = [info valueForKey:UIImagePickerControllerEditedImage];
UIImage *images =info[UIImagePickerControllerEditedImage];
//_HeadShot.image = images;
//NSLog(@"image:%@----info:%a",info);
[_Rightbutton setBackgroundImage: images forState:UIControlStateNormal];
//NSData *fileDara = UIImageJPEGRepresentation(images, 1.0);
//保存到手机
UIImageWriteToSavedPhotosAlbum(images, nil,nil,nil);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end