iOS开发笔记-tabbar图标设成用户头像

1.获取图片,并处理图片存储本地      

    UIImage *head = [UIImage imageWithData:imageData];

                    UIImage* head1 =  [head image:head byScalingToSize:CGSizeMake(30, 30)];

                    UIImage* head2 =  [head image:head byScalingToSize:CGSizeMake(60, 60)];

                    UIImage* head3 =  [head image:head byScalingToSize:CGSizeMake(90, 90)];

//关于处理图片的方法,详见这篇博客 http://blog.csdn.net/jp940110jpjp/article/details/48629431

                    UIImage *resultImage1 = [head1 roundedRectImage:head1 withradius:head1.size.width/2.0 cornerMask:UIImageRoundedCornerTopLeft|UIImageRoundedCornerTopRight|UIImageRoundedCornerBottomLeft|UIImageRoundedCornerBottomRight];

                    UIImage *resultImage2 = [head2 roundedRectImage:head2 withradius:head2.size.width/2.0 cornerMask:UIImageRoundedCornerTopLeft|UIImageRoundedCornerTopRight|UIImageRoundedCornerBottomLeft|UIImageRoundedCornerBottomRight];

                    UIImage *resultImage3 = [head3 roundedRectImage:head3 withradius:head3.size.width/2.0 cornerMask:UIImageRoundedCornerTopLeft|UIImageRoundedCornerTopRight|UIImageRoundedCornerBottomLeft|UIImageRoundedCornerBottomRight];

                    

                    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

                    NSString *home = [paths objectAtIndex:0];

                    //设置一个图片的存储路径,tabbar需要@1x,@2x,@3x的图片所以,我们需要处理成相应的尺寸并存起来

                    NSString *imagePath1 = [home stringByAppendingString:@"/tabbar.png"];

                    NSString *imagePath2 = [home stringByAppendingString:@"/tabbar@2x.png"];

                    NSString *imagePath3 = [home stringByAppendingString:@"/tabbar@3x.png"];

                    

                    //把图片直接保存到指定的路径(同时应该把图片的路径imagePath存起来,下次就可以直接用来取)

                    [UIImagePNGRepresentation(resultImage1) writeToFile:imagePath1 atomically:YES];

                    [UIImagePNGRepresentation(resultImage2) writeToFile:imagePath2 atomically:YES];

                    [UIImagePNGRepresentation(resultImage3) writeToFile:imagePath3 atomically:YES];


2.读取图片并加载

UIImage *tabbarImage;

    UIImage *tabSelectedImage;

if (isLogin) {

            tabbarImage = [JPDealTabImage shoudUseImage];

            tabSelectedImage = tabbarImage;

   }else{

            tabbarImage  = [UIImage imageNamed:@"my_normal.png"];

            tabSelectedImage =[UIImage imageNamed:@"my_selected.png"];

     }


 UIColor *tabbarColor = [UIColor colorWithRed:0xef/255.0 green:0x7f/255.0 blue:0x5e/255.0 alpha:1];

    self.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[tabbarImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[tabSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];

    [self.tabBarItem setTitleTextAttributes:[NSDictionary

                                             dictionaryWithObjectsAndKeys:[UIColor colorWithRed:0x63/255.0 green:0x57/255.0 blue:0x53/255.0 alpha:1],

                                             NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];

    [self.tabBarItem setTitleTextAttributes:[NSDictionary

                                             dictionaryWithObjectsAndKeys:tabbarColor,

                                             NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];





//读取图片

+(UIImage *)shoudUseImage{

//    dispatch_async(dispatch_get_global_queue(0, 0), ^{

        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

        NSString *home = [paths objectAtIndex:0];

        //设置一个图片的存储路径 stringByAppendingString:@/Documents/flower.png

        NSString *imagePath1 = [home stringByAppendingString:@"/tabbar.png"];

        NSString *imagePath2 = [home stringByAppendingString:@"/tabbar@2x.png"];

        NSString *imagePath3 = [home stringByAppendingString:@"/tabbar@3x.png"];

        

        UIImage *head;

        if (WIDTH == 414) {

            head = [UIImage imageWithContentsOfFile:imagePath3];

        }else{

            if (isRetina) {

                head = [UIImage imageWithContentsOfFile:imagePath2];

            }else{

                head = [UIImage imageWithContentsOfFile:imagePath1];

            }

        }

        return head;


//    });

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值