[英]Swift playgrounds with UIImage
I am working with Xcode 6, and I'm trying to recreate the code demoed during session 401 "What's new in Xcode 6".我正在使用 Xcode 6,我正在尝试重新创建会话 401“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.我已将图像添加到 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?我错过了什么吗?
Look at the iOS Developer Library-> Playground Help and 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:喜欢关注:
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 文件夹,我建议使用这种方法,这样资源文件夹就不会经常被删除和重新创建。
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.这是 2014 年 7 月 21 日发布的 Xcode 6.0 beta 4 (6A267N) 的解决方案。我猜这对应于之前的“Inside playground”选项。 That is where the Resources folder is within the playground package itself.这是 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 Contents as follows:使用 Finder - 或者如果你像我一样使用很棒的Path Finder - 右键选择并选择Show Package Contents ,如下所示:
That reveals the packages Resources folder:这显示了包资源文件夹:
Copying the image files into that folder will do the business:将图像文件复制到该文件夹中即可:
let imageNames = ["back-button", "background", "bg_top"]
let images = imageNames.map { UIImage(named: $0) }
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 sample = UIImage(named: "/Users/my_user_name/Desktop/Image1234.jpg")
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.必须使用完整路径似乎比它应该的更复杂,但它有效并且让我继续处理更有趣的问题。
You can find out the path of resourcePath using these commands in playground:您可以在 Playground 中使用这些命令找出 resourcePath 的路径:
var bundle = NSBundle.mainBundle()
var path = bundle.resourcePath
Default for me was:我的默认值是:
/Applications/Xcode6-Beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/Library/Xcode/Agents
I had difficulty getting this setup for an iOS playground, as opposed to an OS X playground.与 OS X 游乐场相比,我很难为 iOS 游乐场设置此设置。 Trying to do it using bundles and relative paths makes it more complicated.尝试使用包和相对路径来做这件事会变得更加复杂。
If you just want to get your hands on an image quickly, you can also just use absolute file path:如果您只想快速获取图像,也可以使用绝对文件路径:
On iOS:在 iOS 上:
# iOS
let absoluteImagePath = "/absolute/path/to/image.png"
let image = UIImage(contentsOfFile: absoluteImagePath)
And on OS X在 OS X 上
# OS X
let absoluteImagePath = "/absolute/path/to/image.png"
let image = NSImage(contentsOfFile: absoluteImagePath)
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 游乐场上,您可以看到捆绑包内的图像:
Access it with either访问它
let img = UIImage(named: "logo.png", inBundle: NSBundle.mainBundle(), compatibleWithTraitCollection: nil)
or或者
let path = NSBundle.mainBundle().pathForResource("logo", ofType: "png") let img = UIImage(contentsOfFile:path)
or in Swift 4:或在 Swift 4 中:
let path = Bundle.main.path(forResource:"logo", ofType: "png") let img = NSImage(contentsOfFile:path!)
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.在与主故事板相同的目录下创建 Playground 文件。
Open Utilities pane for Playground file, and click the right arrow in Resource Path
section to add your images in that directory.打开 Playground 文件的 Utilities 窗格,然后单击Resource Path
部分中的右箭头将图像添加到该目录中。
Test image within Playground file.在 Playground 文件中测试图像。
Using XCode 6.3.1 and running playground in full simulator (iOS) I had to do the following:使用 XCode 6.3.1 并在完整模拟器(iOS)中运行操场,我必须执行以下操作:
Then just instantiate with let i = UIImage(named: "filename.png")
然后用let i = UIImage(named: "filename.png")
实例化
I have no idea why it is not working the easy way with PDF images.我不知道为什么它不能以简单的方式处理 PDF 图像。
I had to add this extension to load PDF images into my Playground:我必须添加此扩展程序才能将 PDF 图像加载到我的 Playground 中:
extension UIImage {
static func fromPDF(_ url: URL) -> UIImage? {
var image: UIImage?
guard
let doc = CGPDFDocument(url as CFURL),
let page = doc .page(at: 1)
else { return nil }
let pageRect = page.getBoxRect(.mediaBox)
let renderer = UIGraphicsImageRenderer(size: pageRect.size)
image = renderer.image { ctx in
UIColor.clear.set()
ctx.fill(pageRect)
ctx.cgContext.translateBy(x: 0.0, y: pageRect.size.height)
ctx.cgContext.scaleBy(x: 1.0, y: -1.0)
ctx.cgContext.drawPDFPage(page)
}
return image
}
}
// Given the file "myImage.pdf" was added to your Playground's Resources folder.
let image = UIImage.fromPDF(#fileLiteral(resourceName: "myImage.pdf"))
It is based on this: https://developer.apple.com/forums/thread/90990它基于此: https : //developer.apple.com/forums/thread/90990
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.