ios 带有 UIImage 的 Swift 游乐场
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24069479/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Swift playgrounds with UIImage
提问by Ross Gibson
I am working with Xcode 6, and I'm trying to recreate the code demoed during session 401 "What's new in Xcode 6". I've added an image to Images.xcassets (called Sample) and within the playground file I'm trying to access this image, as demoed.
我正在使用 Xcode 6,我正在尝试重新创建会话 401“Xcode 6 中的新功能”期间演示的代码。我已将图像添加到 Images.xcassets(称为 Sample),并在 Playground 文件中尝试访问该图像,如演示所示。
My code is as follows (like the demo):
我的代码如下(就像demo一样):
var sample = UIImage(named: "Sample")
However, I can't get it to work like the demo. Am I missing something?
但是,我无法让它像演示一样工作。我错过了什么吗?
回答by Bruce
Look at the iOS Developer Library->Playground Helpand search"Resource Files" and you will find the answer
看iOS开发者库-> Playground帮助,搜索“资源文件”就会找到答案
1、Open the .playground
1、打开.playground
2、Show the Project navigator by selecting View > Navigators > Show Project Navigator.
2、通过选择“视图”>“导航器”>“显示项目导航器”来显示“项目导航器”。
3、Drag the images to the Resources
3、拖拽图片到资源
Like follow:
喜欢关注:
回答by Chris Hill
- Open the .playground file in Finder.
- Create a folder called Resources next to it.
- Add any images you want to this folder.
- In the playground press opt-cmd-1 to open the File Inspector. You should see the playground on the right. If you don't have it selected, press cmd-1 to open the Project Navigator and click on the playground file.
- 在 Finder 中打开 .playground 文件。
- 在它旁边创建一个名为 Resources 的文件夹。
- 将您想要的任何图像添加到此文件夹中。
- 在操场上按 opt-cmd-1 打开文件检查器。您应该会看到右侧的游乐场。如果您没有选择它,请按 cmd-1 打开 Project Navigator 并单击 Playground 文件。
- Under 'Resource Path' choose 'Relative To Playground'
- Click the folder icon underneath and choose the Resources folder created earlier.
- 在“资源路径”下选择“相对于游乐场”
- 单击下方的文件夹图标并选择之前创建的 Resources 文件夹。
You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):
您现在应该有一个可以与标准 NSImage(named:"filename_without_extension") 一起使用的包:
Note: Because Xcode will frequently overwrite the .playground folder, I recommend using this method so the resources folder isn't getting constantly deleted and re-created.
注意:因为 Xcode 会经常覆盖 .playground 文件夹,我建议使用这种方法,这样资源文件夹就不会经常被删除和重新创建。
回答by Max MacLeod
I had some trouble with this also.
我也遇到了一些麻烦。
Unfortunately, Chris' answer didn't work for me. I suspect perhaps a later beta release of Xcode 6 may have removed this setting.
不幸的是,克里斯的回答对我不起作用。我怀疑 Xcode 6 的后续 beta 版本可能已删除此设置。
Here's a solution as of Xcode 6.0 beta 4 (6A267N) available 21st July 2014. I would guess that this corresponds to the "Inside playground" option previously. That is where the Resources folder is within the playground package itself.
这是 2014 年 7 月 21 日发布的 Xcode 6.0 beta 4 (6A267N) 的解决方案。我猜这对应于之前的“Inside playground”选项。这是 Resources 文件夹位于 Playground 包本身的位置。
Here's how to set this up.
这是设置方法。
Using Finder - or if you're like me and use the awesome Path Finder- right select and choose Show Package Contentsas follows:
使用 Finder - 或者如果你像我一样使用很棒的Path Finder- 右键选择并选择Show Package Contents,如下所示:
That reveals the packages Resourcesfolder:
这显示了包资源文件夹:
Copying the image files into that folder will do the business:
将图像文件复制到该文件夹中即可:
let imageNames = ["back-button", "background", "bg_top"]
let images = imageNames.map { UIImage(named: var sample = UIImage(named: "/Users/my_user_name/Desktop/Image1234.jpg")
) }
回答by William Hu
回答by JAL
回答by Caleb
However, I can't get it to work like the demo. Am I missing something?
但是,我无法让它像演示一样工作。我错过了什么吗?
I'm not sure where you need to put the image to refer to it using only the name, but I got the same code to work by specifying the full path to the image, like:
我不确定您需要将图像放在哪里以仅使用名称来引用它,但是通过指定图像的完整路径,我得到了相同的代码,例如:
var bundle = NSBundle.mainBundle()
var path = bundle.resourcePath
Having to use the full path seems more complicated than it should be, but it works and it let me move on to more interesting problems.
必须使用完整路径似乎比它应该的更复杂,但它有效并且让我继续处理更有趣的问题。
回答by user3715448
You can find out the path of resourcePath using these commands in playground:
您可以在 Playground 中使用这些命令找出 resourcePath 的路径:
/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents
Default for me was:
我的默认值是:
# iOS
let absoluteImagePath = "/absolute/path/to/image.png"
let image = UIImage(contentsOfFile: absoluteImagePath)
回答by algal
I had difficulty getting this setup for an iOS playground, as opposed to an OS X playground. Trying to do it using bundles and relative paths makes it more complicated.
与 OS X 游乐场相比,我很难为 iOS 游乐场设置此设置。尝试使用包和相对路径来做这件事会变得更加复杂。
If you just want to get your hands on an image quickly, you can also just use absolute file path:
如果您只想快速获取图像,也可以使用绝对文件路径:
On iOS:
在 iOS 上:
# OS X
let absoluteImagePath = "/absolute/path/to/image.png"
let image = NSImage(contentsOfFile: absoluteImagePath)
And on OS X
在 OS X 上
let img = UIImage(named: "logo.png", inBundle: NSBundle.mainBundle(),
compatibleWithTraitCollection: nil)
回答by voiger
On the iOS playground with XCode 6 beta 5 (and probably later) you can see the image inside the bundle:
在带有 XCode 6 beta 5(可能还有更高版本)的 iOS 游乐场上,您可以看到捆绑包内的图像:
- In the playground press Cmd+Opt+1 and click the arrow under the Resource Path (this will open Finder)
- Put your picture to this folder
Access it with either
let path = NSBundle.mainBundle().pathForResource("logo", ofType: "png") let img = UIImage(contentsOfFile:path)
or
let path = Bundle.main.path(forResource:"logo", ofType: "png") let img = NSImage(contentsOfFile:path!)
or in Swift 4:
let img = UIImage(named: "logo.png", inBundle: NSBundle.mainBundle(), compatibleWithTraitCollection: nil)
- 在 Playground 中按 Cmd+Opt+1 并单击资源路径下的箭头(这将打开 Finder)
- 把你的图片放到这个文件夹
访问它
let path = NSBundle.mainBundle().pathForResource("logo", ofType: "png") let img = UIImage(contentsOfFile:path)
或者
let path = Bundle.main.path(forResource:"logo", ofType: "png") let img = NSImage(contentsOfFile:path!)
或在 Swift 4 中:
##代码##
回答by toadead
This is what worked for me on Xcode Version 6.1.1.
这就是在 Xcode 版本 6.1.1 上对我有用的方法。
Create Playground file under same directory as main storyboard.
Open Utilities pane for Playground file, and click the right arrow in
Resource Path
section to add your images in that directory.Test image within Playground file.
在与主故事板相同的目录下创建 Playground 文件。
打开 Playground 文件的实用程序窗格,然后单击部分中的向右箭头
Resource Path
将图像添加到该目录中。在 Playground 文件中测试图像。