如何剪辑较大的图像以适应以编程方式构建的tabBarController中的tabBar图标。 [英] How to clip larger images to fit into tabBar icons in tabBarController built programmatically.

查看:91
本文介绍了如何剪辑较大的图像以适应以编程方式构建的tabBarController中的tabBar图标。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经以编程方式创建了一个标签控制器。

I have created a tab controller programmatically.

现在,我想将图像添加到我使用的不同标签中:

Now, I wanted to add images to the different tabs, for which I used :

  self.tabBarItem.image = [UIImage imageNamed:@"Sample_Image.png"];

问题是Sample_image的大小比标签要求的大。

The problem is Sample_image is larger in size than is required by tab.

所以我只想知道如何剪裁图像以适应标签。

So just want to know how can I clip the image to fit into tabs.

推荐答案

Sample_image is larger in size than is required by tab.

尝试这段代码,因为这会调整所需图片的大小并返回 UIImage 实例 30x30 大小( UITabbar 所需的大小)。

Try this piece of code as this will resize the required image and return an UIImage instance with 30x30 size (size required for UITabbar).

UIImage *image = [UIImage imageNamed:@"Sample_Image.png"];
self.tabBarItem.image = [self imageWithImage:image scaledToSize:CGSizeMake(30, 30)];

添加此方法

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

这篇关于如何剪辑较大的图像以适应以编程方式构建的tabBarController中的tabBar图标。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆