Status bar app does not show file picker correctly - swift

I'm working on status bar app.
I want to show file picker to select my image.
Here is my code:
Button("Select image") {
let openPanel = NSOpenPanel()
openPanel.prompt = "Select File"
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canCreateDirectories = false
openPanel.canChooseFiles = true
openPanel.allowedFileTypes = ["png","jpg","jpeg"]
openPanel.begin { (result) -> Void in
if result.rawValue == NSApplication.ModalResponse.OK.rawValue {
let selectedPath = openPanel.url!.path
if let nsData = NSData(contentsOfFile: selectedPath) {
imageData = Data(referencing: nsData)
}
}
}
}
It's shown, but behind my app (which is not on status bar)
So how to just show file picker and hide that weird view.

Related

value of type 'NSOpenPanel' has no member 'allowedContentTypes'

If a try to set the allowedContentTypes for an NSOpenPanel, I get always the message that this isn't a member. I cannot use the openPanel.allowedFileTypes because it is deprecated.
I didn't change any setting for the project. It was defined primary as an empty object, and then a added a macOS target
In the source I only import Cocoa
See code:
let openPanel = NSOpenPanel()
openPanel.title = "SelectImageTitle"
openPanel.prompt = "SelectImagePrompt"
openPanel.nameFieldLabel = "SelectImageLabel"
openPanel.showsResizeIndicator = true
openPanel.showsHiddenFiles = false
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
//openPanel.allowedFileTypes = allowedImageTypes
openPanel.allowedContentTypes = [.png]
// openPanel
//----- Perform the selection dialog ----------------------------------------------------------------------------
guard openPanel.runModal() == .OK else { return nil }
return openPanel.url?.path

UIImage and UILabel disappearing after doing UIRefreshControl

UIImages and UILabel are disappearing in some collectionViews after refreshing inside my app.
here is my code. You can see what is wrong in the picture, the left image is before refreshing and there is a discount price label and after refreshing the simulator, it is gone. getProductLatestData(), getBestSellingData() are the Alamofire get request for API.
//For Refresh Controller
//----------------------
var player : AVAudioPlayer
var refreshController : UIRefreshControl = {
let refreshController = UIRefreshControl()
refreshController.addTarget(self, action: #selector(updateData), for: .valueChanged)
return refreshController
}()
//----------------------
//For Update Data when scroll to top
//----------------------------------
#objc func updateData(){
//To play sound when scroll top
//-----------------------------
let pathToSound = Bundle.main.path(forResource: "Flick", ofType: "wav")
let url = URL(fileURLWithPath: pathToSound!)
do {
player = try AVAudioPlayer(contentsOf: url)
player?.play()
} catch let error {
print(error.localizedDescription)
}
//-----------------------------
//Fetch Latest Products Data
getProductLatestData(pageNumber: 0, pageSize: 0)
//Fetch BestSelling Products Data
getBestSellingData(pageNumber: 0, pageSize: 0)
//Fetch Promotion Products Data
getPromotionProductsData(pageNumber: 0, pageSize: 0)
//Fetch Products By Category
getProductsByCat(pageNumber: 0, pageSize: 0)
//Fetch Reward Products
getRewardProducts()
self.refreshController.endRefreshing()
}
if collectionView == latestItemCV {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "latestItemCell", for: indexPath) as! PromotionItemsCollectionViewCell
if let percent = latestItemData[indexPath.row].promotePercent {
if percent != 0 {
cell.percentOff.isHidden = false
cell.percentOff.text = " \(percent) % Off "
cell.percentOffBackground.isHidden = false
}
else{
cell.percentOff.isHidden = true
cell.percentOffBackground.isHidden = true
}
}
if let name = latestItemData[indexPath.row].name {
cell.itemsName.text = name
}
else{
cell.itemsName.text = "-"
}
if let url = latestItemData[indexPath.row].url {
cell.itemsImageView.sd_setImage(with: URL(string: url), placeholderImage: UIImage(named: "placeholder"))
}
//----------------------------------
Thanks to #larme I recheck the code here is how I fixed it, I added those disappeared labels isHidden = false also as I did isHidden = true for them.
if let promotionPrice = latestItemData[indexPath.row].promotePrice {
if promotionPrice != 0 {
cell.originalItemsPrice.text = "\(setCommaForPrice(price: Int(promotionPrice))) Ks"
cell.promotionPercent.isHidden = false
cell.promotionLine.isHidden = false
}
else{
if let orPrice = latestItemData[indexPath.row].originalPrice {
cell.originalItemsPrice.text = "\(setCommaForPrice(price: Int(orPrice))) Ks"
}
cell.promotionPercent.isHidden = true
cell.promotionLine.isHidden = true
}
}

YPImagePicker and sending image and video to another controller

I am trying to send a photo and video from one controller to another with the YPImagePicker pod. I am having an issue understanding how to use this file.
var selectedItems = [YPMediaItem]()
let selectedImageV = UIImageView()
#objc func showPicker() {
var config = YPImagePickerConfiguration()
config.library.mediaType = .photoAndVideo
config.library.onlySquare = true
config.showsFilters = false
config.shouldSaveNewPicturesToAlbum = true
config.albumName = "RecipeSharing"
config.video.compression = AVAssetExportPresetMediumQuality
config.startOnScreen = .library
config.screens = [.library, .photo, .video]
config.video.recordingTimeLimit = 5.0
config.video.libraryTimeLimit = 500.0
config.video.trimmerMaxDuration = 60.0
config.video.trimmerMinDuration = 3.0
config.video.fileType = .mov
config.showsCrop = .rectangle(ratio: (16/9))
config.targetImageSize = YPImageSize.original
config.wordings.libraryTitle = "Gallery"
config.hidesStatusBar = false
config.hidesBottomBar = false
config.library.maxNumberOfItems = 5
let picker = YPImagePicker(configuration: config)
picker.didFinishPicking { [unowned picker] items, _ in
if let photo = items.singlePhoto {
let finalShareVC = NewInstructionsController()
picker.navigationController?.pushViewController(finalShareVC, animated: true)
}
}
picker.didFinishPicking { [unowned picker] items, _ in
if let video = items.singleVideo {
let finalShareVC = NewInstructionsController()
picker.navigationController?.pushViewController(finalShareVC, animated: true)
}
}
present(picker, animated: true, completion: nil)
}
My Second Controller has the the code as the following.
var selectedImage: UIImage? {
didSet {
guard let image = selectedImage else { return }
instructionsImage.image = image
}
}
This is used to receive the file the photo I believe. Does anyone know what I have to use to send a photo and video to another controller so that it can be used. I have tried reading a lot on this pod. I get to the save option in this pod and then it does not do anything. I was wondering if anyone had an idea or could help me. Maybe I dont need to push the view but I am unsure. Thank you.

CGImageDestinationCreateWithURL return nill value

I am developing a MacOS app for to edit metadata of image. For this purpose, I use CGImageDestinationCreateWithURL function for to create destination to the file located in 'Documents' or 'Desktop' but it returns nil value. I checked paremeters of the function (URL and file type) several times but can not find reason of the issue.
Here is the code
#IBAction func browseFile(_ sender: AnyObject) {
let dialog = NSOpenPanel()
dialog.title = "Choose a .txt file"
dialog.showsResizeIndicator = true
dialog.showsHiddenFiles = false
dialog.canChooseDirectories = true
dialog.canCreateDirectories = true
dialog.allowsMultipleSelection = false
dialog.allowedFileTypes = nil
if dialog.runModal() == NSApplication.ModalResponse.OK {
let result = dialog.url // Pathname of the file
if result != nil {
let path = result!.path
filename_field.stringValue = path
let imageSource = CGImageSourceCreateWithURL(result as! CFURL, nil)
let type = CGImageSourceGetType(imageSource!)
let uurl = NSURL(fileURLWithPath: path)
// here I am trying to create a destination but it returns nil
var imageDest: CGImageDestination = CGImageDestinationCreateWithURL(uurl, type!, 1, nil)!
}
} else {
// User clicked on "Cancel"
return
}
}

NSOpenPanel as sheet

Ive looked around at other answers, but nothing seems to be helping my case.
I have a viewController class which contains an IBAction for a button. This button should open a NSOpenPanel as a sheet from that viewController:
class ViewController: NSViewController {
#IBAction func folderSelection(sender: AnyObject) {
var myFiledialog: NSOpenPanel = NSOpenPanel()
myFiledialog.prompt = "Select path"
myFiledialog.worksWhenModal = true
myFiledialog.allowsMultipleSelection = false
myFiledialog.canChooseDirectories = true
myFiledialog.canChooseFiles = false
myFiledialog.resolvesAliases = true
//myFiledialog.runModal()
myFiledialog.beginSheetModalForWindow(self.view.window!, completionHandler: nil)
var chosenpath = myFiledialog.URL
if (chosenpath!= nil)
{
var TheFile = chosenpath!.absoluteString!
println(TheFile)
//do something with TheFile
}
else
{
println("nothing chosen")
}
}
}
The problem comes from myFileDialog.beginSheetModalForWindow(..) , it works with the line above, but that is not a sheet effect
You need to call beginSheetModalForWindow from your panel on your window, and use a completion block:
let myFiledialog = NSOpenPanel()
myFiledialog.prompt = "Select path"
myFiledialog.worksWhenModal = true
myFiledialog.allowsMultipleSelection = false
myFiledialog.canChooseDirectories = true
myFiledialog.canChooseFiles = false
myFiledialog.resolvesAliases = true
myFiledialog.beginSheetModalForWindow(window, completionHandler: { num in
if num == NSModalResponseOK {
let path = myFiledialog.URL
print(path)
} else {
print("nothing chosen")
}
})
Swift 5
let dialog = NSOpenPanel()
dialog.beginSheetModal(for: self.view.window!){ result in
if result == .OK, let url = dialog.url {
print("Got", url)
}
}