Swift playgrounds with UIImage

2019-01-13 05:43发布

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.

My code is as follows (like the demo):

var sample = UIImage(named: "Sample")

However, I can't get it to work like the demo. Am I missing something?

11条回答
一夜七次
2楼-- · 2019-01-13 06:08

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.

查看更多
聊天终结者
3楼-- · 2019-01-13 06:08

On the iOS playground with XCode 6 beta 5 (and probably later) you can see the image inside the bundle:

  1. In the playground press Cmd+Opt+1 and click the arrow under the Resource Path (this will open Finder)
  2. Put your picture to this folder
  3. 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:

    let path = Bundle.main.path(forResource:"logo", ofType: "png")
    let img = NSImage(contentsOfFile:path!)
    
查看更多
该账号已被封号
4楼-- · 2019-01-13 06:12
  1. Open the .playground file in Finder.
  2. Create a folder called Resources next to it.
  3. Add any images you want to this folder.
  4. 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.

File Inspector

  1. Under 'Resource Path' choose 'Relative To Playground'
  2. Click the folder icon underneath and choose the Resources folder created earlier.

You should now have a bundle that you can use with the standard NSImage(named:"filename_without_extension"):

Working nsbundle image

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.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-13 06:15

Look at the iOS Developer Library->Playground Help and search"Resource Files" and you will find the answer

1、Open the .playground

2、Show the Project navigator by selecting View > Navigators > Show Project Navigator.

3、Drag the images to the Resources

Like follow:

enter image description here

查看更多
We Are One
6楼-- · 2019-01-13 06:16

This is what worked for me on Xcode Version 6.1.1.

  1. Create Playground file under same directory as main storyboard.

    Folder Structure

  2. Open Utilities pane for Playground file, and click the right arrow in Resource Path section to add your images in that directory.

    Resource Path for Playground

  3. Test image within Playground file.

    Result

查看更多
登录 后发表回答