Populating NSImage in Interface Builder from Folder Reference - swift

I placed a folder with images into my project as Folder Reference. Now I can see it in my Package in Contents/Resources/Images/foo.jpg
I try to populate an NSImage like this:
#IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
imageView.image = NSImage (named: "Images/foo.jpg")
}
But no image is shown when the app is loaded. I am using interface builder not XIB. Does it make a difference? If I add the images as Group I can use named:"foo.jpg" without any issue.

I understand now the inner workings. This is my solution:
#IBOutlet var imageView: NSImageView!
override func viewDidLoad() {
super.viewDidLoad()
let image = Bundle.main.path(forResource: "foo", ofType: "jpg", inDirectory: "Images")
imageView.image = NSImage (contentsOfFile: image)
}
The reference files are not named in my bundle so they are never found. With the full path it works well.

Related

xcode 12 + Swift + UIImageView

Since I have opened my project with xcode 12 I am getting the following issue:
import UIKit
class PersonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
let carImage = UIImage(named: "carImg")
imageView.image = carImage
}
}
The error I am getting is: "No exact matches in reference to instance method 'image"
The compiler is basically not recognising that UIImageView has a property called "image" or that UIKit is not imported.
If I create a new project this compiles just fine, so I am guessing it is some sort of setting that was fine in xcode11 but not fine in xcode12.
Note: This is an Objective C project with a few classes in Swift.
Note: Restarted my mac, did clean project and removed Derived Data
Are you missing an import of UIKit at the top of your file?
import UIKit
class PersonViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let imageView = UIImageView()
let carImage = UIImage(named: "carImg")
imageView.image = carImage
}
}
class PersonViewController: UIViewController {
#IBOutlet weak var imageView: UIImageView! //This line is important and must not be excluded

Loading correct image to detail view

I have a TableView app that plays music. The tracks are named: 0.mp3, 1.mp3 etc. They play in a detail view where I also want an image of the cover. I named the cover images: 0.png, 1.png etc.
I tried to load the correct image with the code:
self.bookImage.image = UIImage(named: ("/\(trackID!).png"))
but it doesn't work.
Here's some more of my code:
import UIKit
import AVFoundation
class AudioPlayerVC: UIViewController {
var trackID: Int!
var audioPlayer:AVAudioPlayer!
#IBOutlet var bookImage: UIImageView!
#IBAction func play(_ sender: AnyObject) {
if !audioPlayer.isPlaying{
audioPlayer.play()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.bookImage.image = UIImage(named: ("/\(trackID!).png"))
trackLbl.text = "Track \(trackID!)"
let path: String! = Bundle.main.resourcePath?.appending("/\(trackID!).mp3")
let mp3URL = NSURL(fileURLWithPath: path)
do
{
audioPlayer = try AVAudioPlayer(contentsOf: mp3URL as URL)
audioPlayer.play()
}
}
}
You need
self.bookImage.image = UIImage(named:"\(trackID).png")
Or
self.bookImage.image = UIImage(named:"\(trackID)")
First, you don't need / character after opening quotation mark.
self.bookImage.image = UIImage(named:"\(trackID).png")
Should work just fine.
1) Please add your images to Assets.xcassets
2) Name images like 0,1,2,3 in Assets.xcassets
3) Write a code:
let trackId = String(trackId)
self.bookImage.image = UIImage(named: trackId)

Cocoa - PDFView/ThumbnailView dragging

I have a PDFView and a PDFThumbnailView and implemented it this way:
class PDFViewController: NSViewController {
#IBOutlet weak var pdfView: PDFView!
#IBOutlet weak var pdfThumbnailView: PDFThumbnailView!
override func viewDidLoad() {
super.viewDidLoad()
// Do view setup here.
let doc = PDFDocument(data: NSData(contentsOfFile: "file.pdf")!)
pdfView.setDocument(doc)
pdfView.setAllowsDragging(true)
pdfThumbnailView.setPDFView(pdfView)
pdfThumbnailView.setAllowsDragging(true)
}
}
Since the documentation says:
Sets whether users can drag thumbnails within the thumbnail view; that
is, re-order pages in the document.
I thought this would do it, but it isn't doing anything. I can't drag and drop and so reorder the document.
Connect pdfThumbnailView and pdfView before setting the document, in the xib or in code.

cannot view pdf to web view using file path

i have an app to download pdf file. If the download completed it have a path like
file:///Users/developer/Library/Developer/CoreSimulator/Devices/F9AB746D-20C3-4333-AE70-2840538F4AED/data/Containers/Data/Application/861FA32B-06AA-4176-A499-B4BB8DF224C1/Library/Caches/pdffile.pdf
I try to view using this
#IBOutlet weak var webView: UIWebView!
var path = ""
var receiveData: String = "" //this contain file:///Users/developer.../Caches/pdffile.pdf
override func viewDidLoad() {
super.viewDidLoad()
let url = NSURL.fileURLWithPath(receiveData)
print(url)
self.webView.loadRequest(NSURLRequest(URL: url))
}
the build succeded and no error butstill can't view the pdf file
what's wrong?
Change your NSURL path, you need to use receiveData, because that already contain the path of the file, NSBundle is use when you have file inside your project directory.
let url = NSURL.fileURLWithPath(receiveData)

UIWebView is not displaying (Swift)

I am just trying to create a simple webview inside a view controller.
Here is my code:
import UIKit
class test:UIViewController{
#IBOutlet weak var myWebView: UIWebView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var myString = "http://www.google.com"
let myURL = NSURL(string: myString)
let myReq = NSURLRequest(URL: myURL)
myWebView.loadRequest(myReq)
println(myWebView.loading)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
I'm not getting any errors, simply a white screen. The loading check returns false. It seems like it should be the outlet connection, but I am sure I've made the proper connection. I ctrl-dragged the blue UIWebView element from storyboard into the class as the UIWebView! object and named it myWebView. The circle is filled in on the swift file indicating a connection, and they match up in the connections on the storyboard.
This code originally worked fine for me, but now it does not. I tried creating a completely new project and it still is not working. Adding the UIWebViewDelegate protocol to the class didn't work I've looked at the solutions on the following links, but they do not apply:
link1
link2
I'm out of ideas. Anyone got anything?
Thanks!
This is what you wan in viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
var myWeb = UIWebView(frame: CGRect(x: 40, y: 100, width: 240, height: 128))
var myString = "http://www.google.com"
let myURL = NSURL(string: myString)
let myReq = NSURLRequest(URL: myURL!)
myWeb.loadRequest(myReq)
view.addSubview(myWeb)
println(myWeb.loading)
}
Be careful, you have another web view (called myWebView) from the storyboard, as an outlet and that is not used. By writing the above code you create a new web view programmatically (called myWeb).