• Images.xcassets 的简单使用


    总结起来Images.xcassets  就这么几句话
    (1)以下開始简单整理
    进入images.xcassets
    
    右键--->New Image Set
    或者点XCode的images.xcassets界面左下有个+号, 新增New Image Set
    
    为新增的Set改一个名字比方user_icon
    把你的图片直接拖到这里面相应的
    1X, 2X, 3X中去
    1X是iP4的图片, 2X是5和6的图片, 3X是6+的图片
    
    然后直接使用你定义的这个user_icon就可以
    比方
    UIImage *img = UIImage imageNamed:"user_icon"

    
    
    (2)详情分析


    新建项目。例如以下所看到的:

    本文分享一下Images.xcassets的体验~_~

    1. 打开此前使用过的HelloWorld项目,然后单击并打开导航区域中的Images.xcassets,看看都有些什么东东:]:

     

    2. 在图中能够看到中间位置有两个虚线框,感觉应该能够直接拖文件进来。

    OK,那就先准备一下资源文件。例如以下图所看到的:


    说明:为方便起见。除Icon7.png之外。其它图标的文件名称均沿袭了以往iOS图标的命名规则。

    3. 将Icon-Small@2x.png拖拽到第一个虚线框中,将Icon7.png拖拽到第二个虚线框中。例如以下图所看到的:


     

    说明Icon-Small@2x.png的尺寸是58*58像素的,而Icon7.png的尺寸是120*120像素的。另外,假设拖入的图片尺寸不对,Xcode会提示警告信息。

    4. 上图中单击有用工具区域的最右側Show the Attributes inspector(显示属性检查器)图标。可以看到图像集的属性。勾选一下iOS 6.1 and Prior Sizes看看会发生什么变化?


     

    5. 分别将Icon-Small.pngIcon.pngIcon@2x.png顺序拖拽到三个空白的虚线框中,完毕之后的效果例如以下图所看到的:


     

    6. 右击左側的AppIcon,在弹出的辅助菜单中选择Show in Finder。看看刚才拖拽都做了哪些工作:



     

    7. 图中除了Contents.json这个文件陌生之外。其它文件都是刚刚拖拽进Xcode的。双击查看一下Contents.json文件内容:

    [html] view plaincopy
    1. {  
    2.  "images" : [  
    3.     {  
    4.      "size" : "29x29",  
    5.      "idiom" : "iphone",  
    6.      "filename" : "Icon-Small.png",  
    7.      "scale" : "1x"  
    8.    },  
    9.     {  
    10.      "size" : "29x29",  
    11.      "idiom" : "iphone",  
    12.      "filename" : "Icon-Small@2x.png",  
    13.      "scale" : "2x"  
    14.    },  
    15.     {  
    16.      "size" : "57x57",  
    17.      "idiom" : "iphone",  
    18.      "filename" : "Icon.png",  
    19.      "scale" : "1x"  
    20.    },  
    21.     {  
    22.      "size" : "57x57",  
    23.      "idiom" : "iphone",  
    24.      "filename" : "Icon@2x.png",  
    25.      "scale" : "2x"  
    26.    },  
    27.     {  
    28.      "size" : "60x60",  
    29.      "idiom" : "iphone",  
    30.      "filename" : "Icon7.png",  
    31.      "scale" : "2x"  
    32.     }  
    33.   ],  
    34.  "info" : {  
    35.    "version" : 1,  
    36.    "author" : "xcode"  
    37.   }  
    38. }  

    内容一目了然啊,哈哈,以后再也不用去特意记住每一个尺寸的图标分别应该叫什么名字了。不知道您会不会,反正我每次都是粘贴复制的,呵呵。以后,仅仅要通过拖拖拽拽就搞定了~_~

    8. 图标搞定了,启动图片照做就OK了。详细操作区别不大。完毕之后的示意图例如以下:


     

    9. 再看一下Finder中的内容,例如以下所看到的:


     

    10. 在Finder中不难发现多出了两个文件,各自是:Default@2x-1.pngDefault-568h@2x-1.png,双击打开相应的Contents.json文件。内容例如以下:

    [html] view plaincopy
    1. {  
    2.  "images" : [  
    3.     {  
    4.      "orientation" : "portrait",  
    5.      "idiom" : "iphone",  
    6.      "extent" : "full-screen",  
    7.      "minimum-system-version" : "7.0",  
    8.      "filename" : "Default@2x.png",  
    9.      "scale" : "2x"  
    10.    },  
    11.     {  
    12.      "extent" : "full-screen",  
    13.      "idiom" : "iphone",  
    14.      "subtype" : "retina4",  
    15.      "filename" : "Default-568h@2x.png",  
    16.      "minimum-system-version" : "7.0",  
    17.      "orientation" : "portrait",  
    18.      "scale" : "2x"  
    19.    },  
    20.     {  
    21.      "orientation" : "portrait",  
    22.       "idiom" : "iphone",  
    23.      "extent" : "full-screen",  
    24.      "filename" : "Default.png",  
    25.      "scale" : "1x"  
    26.    },  
    27.     {  
    28.      "orientation" : "portrait",  
    29.      "idiom" : "iphone",  
    30.      "extent" : "full-screen",  
    31.      "filename" : "Default@2x-1.png",  
    32.       "scale" : "2x"  
    33.    },  
    34.     {  
    35.      "orientation" : "portrait",  
    36.      "idiom" : "iphone",  
    37.      "extent" : "full-screen",  
    38.      "filename" : "Default-568h@2x-1.png",  
    39.      "subtype" : "retina4",  
    40.      "scale" : "2x"  
    41.     }  
    42.   ],  
    43.  "info" : {  
    44.    "version" : 1,  
    45.    "author" : "xcode"  
    46.   }  
    47. }  

    11. 将当中的"filename": "Default@2x-1.png""filename" : "Default-568h@2x-1.png"分别改为"filename": "Default@2x.png""filename" : "Default-568h@2x.png",保存并返回Xcode看看会发生什么?


    改动后的Contents.json内容例如以下:

    [html] view plaincopy
    1. {  
    2.  "images" : [  
    3.     {  
    4.      "orientation" : "portrait",  
    5.      "idiom" : "iphone",  
    6.      "extent" : "full-screen",  
    7.      "minimum-system-version" : "7.0",  
    8.      "filename" : "Default@2x.png",  
    9.      "scale" : "2x"  
    10.    },  
    11.     {  
    12.      "extent" : "full-screen",  
    13.      "idiom" : "iphone",  
    14.      "subtype" : "retina4",  
    15.      "filename" : "Default-568h@2x.png",  
    16.      "minimum-system-version" : "7.0",  
    17.      "orientation" : "portrait",  
    18.      "scale" : "2x"  
    19.    },  
    20.     {  
    21.      "orientation" : "portrait",  
    22.       "idiom" : "iphone",  
    23.      "extent" : "full-screen",  
    24.      "filename" : "Default.png",  
    25.      "scale" : "1x"  
    26.    },  
    27.     {  
    28.      "orientation" : "portrait",  
    29.      "idiom" : "iphone",  
    30.      "extent" : "full-screen",  
    31.      "filename" : "Default@2x.png",  
    32.       "scale" : "2x"  
    33.    },  
    34.     {  
    35.      "orientation" : "portrait",  
    36.      "idiom" : "iphone",  
    37.      "extent" : "full-screen",  
    38.      "filename" : "Default-568h@2x.png",  
    39.      "subtype" : "retina4",  
    40.      "scale" : "2x"  
    41.     }  
    42.   ],  
    43.  "info" : {  
    44.    "version" : 1,  
    45.    "author" : "xcode"  
    46.   }  
    47. }  

    12. 分别选中下方的"Default@2x-1.png"和"Default-568h@2x-1.png",按删除键删除这两个文件,删除之后的效果例如以下图所看到的:


    删除之后Finder中的内容例如以下所看到的:

    13. 接下来我们新建一个图像试试看怎样操作。開始之前我们仍然须要准备一下素材。例如以下图所看到的:


    说明:为了方便在执行时看出不同分辨率的设备使用的背景图片不同,我在素材图片中添加了文字标示。

    14. 将准备好的三个Background直接拖拽到Xcode中,完毕之后例如以下图所看到的:


     

    15. 单击右側Devices中的Universal。并选择Device Specific,然后在下方勾选iPhoneRetina 4-inch,同一时候取消勾选iPad,完毕之后例如以下图所看到的:

     

    16. 将下方Unassigned中的图片直接拖拽到右上角R4位置,设置视网膜屏使用的背景图片,例如以下图所看到的:

     

    17. 单击并打开Main.storyboard。选中左側的View Controller,然后在右側File Inspector中。取消勾选Use Autolayout选项,例如以下图所看到的:

     

    18. 从右側工具栏中拖拽一个UIImageViewView Controller主视图中。处于其它控件的最底层

    同一时候调整该UIImageView的尺寸属性,例如以下图所看到的:

     

    19. 设置该UIImageView使用的图像,例如以下图所看到的:

     

    20. 在不同屏幕的模拟器上执行HelloWorld应用。能够看到例如以下三张图示。




     

    OK。Images.xcassets的初体验一文至此算是告一段落。现做一下简单的小节:

    1. 有过Xcode曾经版本号使用经验的朋友应该会发现,从Xcode 5開始已经无需再去记住Icon.png和Default.png针对不同分辨率使用的文件名称了;

    2. Xcode 5针对4存视网膜屏的图像提供了单独的支持,攻克了以往在兼容四存屏时。有时不得不须要编写专门的代码载入不同的图片;

    3. Image.xcassets更加便于管理和维护;

     注意:LaunchScreen 启动图片的PNG图片不要使用透明图片。非常丑的。呵呵。

  • 相关阅读:
    Java反射机制实现与原理
    Android APK反编译详解
    常见排序算法(二)
    常见排序算法(一)
    Error:Execution failed for task ':app:clean'. > Unable to delete directory: /media/file/workspaces/a
    Cannot run program "/home/user/Android/Sdk/build-tools/23.0.2/aapt": error=2 No such file or directo
    解决Ubuntu下IDEA无法输入中文问题
    ava method "org.apache.struts2.components.Form.getValidators(String)" threw an exception when invoke
    用C语言代码判别CPU的大小端模式
    (转)c语言随机数srandom( )
  • 原文地址:https://www.cnblogs.com/blfbuaa/p/7381094.html
Copyright © 2020-2023  润新知