换取头像

iOS头像选择器实现
本文介绍了一个iOS应用中实现的用户头像选择功能,包括从相册选取和使用相机拍摄两种方式,并详细展示了如何通过代码实现这些功能。
#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    UIImageView *UserHeadImage;
}

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    
    UIImageView *headBackImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, Main_Width, widget_height(270))];
    [headBackImage setImage:[UIImage imageNamed:@"Mine_Background"]];
    headBackImage.userInteractionEnabled = YES;
    [self.view addSubview:headBackImage];


    //用户默认头像
    UIButton *headBtn = [[UIButton alloc]initWithFrame:CGRectMake((Main_Width - widget_height(169))/2, widget_height(35), widget_height(169), widget_height(169))];
    
    [headBackImage addSubview:headBtn];
    UserHeadImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, widget_height(169), widget_height(169))];
   [UserHeadImage setImage:[UIImage imageNamed:@"Mine_UserHead"]];
    UserHeadImage.layer.masksToBounds = YES;
    UserHeadImage.layer.cornerRadius = widget_height(169)/2;
    [headBackImage addSubview:headBtn];
    
    [headBtn addSubview:UserHeadImage];
    
    [headBtn addTarget:self action:@selector(tapToUserInfoPage) forControlEvents:UIControlEventTouchUpInside];
    
   
    
}
- (void)tapToUserInfoPage {
    
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE_8_0
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"请选择" message:nil preferredStyle: UIAlertControllerStyleActionSheet];
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        UIAlertAction *photoLibraryAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self getImageFromPhotoLibrary];
        }];
        UIAlertAction *cameraAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self getImageFromCamera];
            
        }];
        [alertController addAction:cancelAction];
        [alertController addAction:photoLibraryAction];
        [alertController addAction:cameraAction];
        
        [self presentViewController:alertController animated:YES completion:NULL];
    
#endif

    #if __IPHONE_OS_VERSION_MAX_ALLOWED < _IPHONE_8_0
       UIActionSheet *alertView = [[UIActionSheet alloc] initWithTitle:@"请选择" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"相册",@"相机", nil];
        
        
        
        [alertView showInView:self.view];
    #endif
        


}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex) {
        case 0:
            [self getImageFromPhotoLibrary];
            break;
        case 1:
            [self getImageFromCamera];
            break;
        default:
            break;
    }
}
//从相册获取
- (void)getImageFromPhotoLibrary
{
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    [picker.navigationBar setBackgroundImage:[UIImage imageNamed:@"partner_navi"] forBarMetrics:UIBarMetricsDefault];
    [picker.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName, nil]];
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    picker.delegate = self;
    //设置选择后的图片可被编辑
    picker.allowsEditing = YES;
    [self presentViewController:picker animated:YES completion:NULL];
}
//调用相机
- (void)getImageFromCamera
{
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    
    
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
    {
        
        imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
        
        imagePicker.allowsEditing = YES;
        imagePicker.delegate = self;
        
        
        [self presentViewController:imagePicker animated:YES completion:NULL];
        
    }
    else
    {
//        UIAlertController*alert = [UIAlertController alertControllerWithTitle:@"tishi" message:@"相机不能用" preferredStyle:UIAlertControllerStyleAlert];
//        UIAlertAction *confirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action){}];[alert addAction:confirm];
//        [self presentViewController:alert animated:YES completion:nil];
        
        [self creatAlert:@"相机不能用"];
        

    }
}
#pragma mark - image picker delegte   调用系统相机

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //获取图片
    UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
    UserHeadImage.image = image;
    
    
    
    [self dismissViewControllerAnimated:YES completion:NULL];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)creatAlert:(NSString *)msg
{
#if __IPHONE_OS_VERSION_MAX_ALLOWED < _IPHONE_8_0
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示"
                                                   message:msg
                                                  delegate:nil
                                         cancelButtonTitle:@"确定"
                                         otherButtonTitles:nil];
    [alert show];
#endif
    
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= _IPHONE_8_0
    UIAlertController *avc = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [avc addAction:action];
    [self presentViewController:avc animated:YES completion:^{
        
    }];
#endif
}

@end

#ifndef Prefix_pch

#define Prefix_pch

 

//获取屏幕高宽

#define Main_Height      [[UIScreen mainScreen] bounds].size.height

#define Main_Width       [[UIScreen mainScreen] bounds].size.width

 

#define widget_width(w)  ((w) * Main_Width / 750)

#define widget_height(h) ((h) * Main_Height / 1334)

 

 

#endif /* Prefix_pch */

 

转载于:https://www.cnblogs.com/CY-miaomiako/p/5440059.html

(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值