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

来源:互联网 发布:淘宝卖家套餐怎么设置 编辑:程序博客网 时间:2024/05/18 02:03

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
原创粉丝点击