Getting currently focused NSTextView in SwiftUI from WindowGroup's CommandGround-item - macos

Question
I've implemented a NSTextView SwiftUI-wrapper (following this great example). There are several of these NSTextViews on my view. In the app's menu, there is a button that should change the currently focused NSTextView's content, e.g:
Is there a way to determine which NSTextView is currently focused? In my current solution, I resorted to storing the NSTextView in a global view model's variable by passing the NSTextView when its "becomeFirstResponder" is called.
However, I'm afraid this solution could either lead to retain cycles or to the NSTextView stored in the view model becoming nil. Is there a cleaner way of doing this? Any help is appreciated!
Current solution/Code
NSTextView
struct TextArea: NSViewRepresentable {
// Source : https://stackoverflow.com/a/63761738/2624880
#Binding var text: NSAttributedString
#Binding var selectedRange: NSRange
#Binding var isFirstResponder: Bool
func makeNSView(context: Context) -> NSScrollView {
context.coordinator.createTextViewStack()
}
func updateNSView(_ nsView: NSScrollView, context: Context) {
if let textArea = nsView.documentView as? NSTextView {
textArea.textStorage?.setAttributedString(self.text)
if !(self.selectedRange.location == textArea.selectedRange().location && self.selectedRange.length == textArea.selectedRange().length) {
textArea.setSelectedRange(self.selectedRange)
}
// Set focus (SwiftUI 👉 AppKit)
if isFirstResponder {
nsView.becomeFirstResponder()
DispatchQueue.main.async {
if ViewModel.shared.focusedTextView != textArea {
ViewModel.shared.focusedTextView = textArea
}
}
} else {
nsView.resignFirstResponder()
}
}
}
func makeCoordinator() -> Coordinator {
Coordinator(text: $text, selectedRange: $selectedRange, isFirstResponder: $isFirstResponder)
}
class Coordinator: NSObject, NSTextViewDelegate {
var text: Binding<NSAttributedString>
var selectedRange: Binding<NSRange>
var isFirstResponder: Binding<Bool>
init(text: Binding<NSAttributedString>,
selectedRange: Binding<NSRange>,
isFirstResponder: Binding<Bool>) {
self.text = text
self.selectedRange = selectedRange
self.isFirstResponder = isFirstResponder
}
func textView(_ textView: NSTextView, shouldChangeTextIn range: NSRange, replacementNSAttributedString text: NSAttributedString?) -> Bool {
defer {
self.text.wrappedValue = textView.attributedString()
self.selectedRange.wrappedValue = textView.selectedRange()
}
return true
}
fileprivate lazy var textStorage = NSTextStorage()
fileprivate lazy var layoutManager = NSLayoutManager()
fileprivate lazy var textContainer = NSTextContainer()
fileprivate lazy var textView: NSTextViewWithFocusHandler = NSTextViewWithFocusHandler(frame: CGRect(), textContainer: textContainer)
fileprivate lazy var scrollview = NSScrollView()
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
self.text.wrappedValue = NSAttributedString(attributedString: textView.attributedString())
self.selectedRange.wrappedValue = textView.selectedRange()
}
func textViewDidChangeSelection(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
DispatchQueue.main.async {
if !(self.selectedRange.wrappedValue.location == textView.selectedRange().location && self.selectedRange.wrappedValue.length == textView.selectedRange().length) {
self.selectedRange.wrappedValue = textView.selectedRange()
}
}
}
func textDidBeginEditing(_ notification: Notification) {
DispatchQueue.main.async {
self.isFirstResponder.wrappedValue = true
}
}
func textDidEndEditing(_ notification: Notification) {
DispatchQueue.main.async {
self.isFirstResponder.wrappedValue = false
}
}
func createTextViewStack() -> NSScrollView {
let contentSize = scrollview.contentSize
textContainer.containerSize = CGSize(width: contentSize.width, height: CGFloat.greatestFiniteMagnitude)
textContainer.widthTracksTextView = true
textView.minSize = CGSize(width: 0, height: 0)
textView.maxSize = CGSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude)
textView.isVerticallyResizable = true
textView.frame = CGRect(x: 0, y: 0, width: contentSize.width, height: contentSize.height)
textView.autoresizingMask = [.width]
textView.delegate = self
scrollview.borderType = .noBorder
scrollview.hasVerticalScroller = true
scrollview.documentView = textView
scrollview.layer?.cornerRadius = 10
scrollview.drawsBackground = false
textStorage.addLayoutManager(layoutManager)
layoutManager.addTextContainer(textContainer)
return scrollview
}
}
}
class NSTextViewWithFocusHandler: NSTextView {
override func becomeFirstResponder() -> Bool {
// ⚠️ Set self as currently focused TextView (AppKit 👉 SwiftUI)
ViewModel.shared.focusedTextView = self
return super.becomeFirstResponder()
}
}
ViewModel
class ViewModel: ObservableObject {
static let shared = ViewModel()
#Published var attributedTextQuestion: NSAttributedString = NSAttributedString(string: "Initial value question")
#Published var attributedTextAnswer: NSAttributedString = NSAttributedString(string: "Initial value answer")
#Published var selectedRangeQuestion: NSRange = NSRange()
#Published var selectedRangeAnswer: NSRange = NSRange()
#Published var questionFocused: Bool = false // Only works in direction SwiftUI 👉 AppKit
#Published var answerFocused: Bool = false // (dito)
weak var focusedTextView: NSTextView? {didSet{
DispatchQueue.main.async {
self.menuItemEnabled = self.focusedTextView != nil
}
}}
#Published var menuItemEnabled: Bool = false
}
ContentView
struct ContentView: View {
#ObservedObject var model: ViewModel
var body: some View {
VStack {
Text("Question")
TextArea(text: $model.attributedTextQuestion,
selectedRange: $model.selectedRangeQuestion,
isFirstResponder: $model.questionFocused)
Text("Answer")
TextArea(text: $model.attributedTextAnswer,
selectedRange: $model.selectedRangeAnswer,
isFirstResponder: $model.answerFocused)
}
.padding()
}
}
App
#main
struct TextViewMacOSSOFrageApp: App {
#ObservedObject var model: ViewModel = ViewModel.shared
var body: some Scene {
WindowGroup {
ContentView(model: model)
}.commands {
CommandGroup(replacing: .textFormatting) {
Button(action: {
// ⚠️ The currently focused TextView is retrieved and its AttributedString updated
guard let focusedTextView = model.focusedTextView else { return }
let newAttString = NSMutableAttributedString(string: "Value set through menu item")
newAttString.addAttribute(.backgroundColor, value: NSColor.yellow, range: NSRange(location: 0, length: 3))
focusedTextView.textStorage?.setAttributedString(newAttString)
focusedTextView.didChangeText()
}) {
Text("Insert image")
}.disabled(!model.menuItemEnabled)
}
}
}
}

Related

SwiftUI Invoke NSPopover with Keyboard Shortcut

I'm building a menu bar application with SwiftUI for macOS Big Sur and can't figure out how to open the popover (the app's main window, since it's a menu bar app) with a keyboard shortcut. I want users to be able to view the window by pressing Command + [a letter] regardless of what else they're doing on their computer (as long as the application is open of course). Here are the main functions and code that control the popover:
#main
struct MenuBarPopoverApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
Settings{
EmptyView()
}
.commands {
MenuBarPopoverCommands(appDelegate: appDelegate)
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
var popover = NSPopover.init()
var statusBarItem: NSStatusItem?
var contentView: ContentView!
override class func awakeFromNib() {}
func applicationDidFinishLaunching(_ notification: Notification) {
print("Application launched")
NSApplication.shared.activate(ignoringOtherApps: true)
contentView = ContentView()
popover.animates = false
popover.behavior = .transient
let contentVc = NSViewController()
contentVc.view = NSHostingView(rootView: contentView.environmentObject(self))
popover.contentViewController = contentVc
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let itemImage = NSImage(named: "statusBarIcon")
itemImage?.isTemplate = true
statusBarItem?.button?.image = itemImage
statusBarItem?.button?.action = #selector(AppDelegate.togglePopover(_:))
}
#objc func showPopover(_ sender: AnyObject?) {
if let button = statusBarItem?.button {
NSApplication.shared.activate(ignoringOtherApps: true)
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
}
#objc func closePopover(_ sender: AnyObject?) {
popover.performClose(sender)
}
#objc func togglePopover(_ sender: AnyObject?) {
if popover.isShown {
closePopover(sender)
} else {
showPopover(sender)
}
}
}
And the MenuBarPopoverCommands (the main part of the app is a text editor, so I have a bunch of keyboard shortcuts relating to that):
struct MenuBarPopoverCommands: Commands {
let appDelegate: AppDelegate
init(appDelegate: AppDelegate) {
self.appDelegate = appDelegate
}
var body: some Commands {
CommandMenu("Edit") {
Section {
Button("Cut") {
appDelegate.contentView.editCut()
}.keyboardShortcut(KeyEquivalent("x"), modifiers: .command)
Button("Copy") {
appDelegate.contentView.editCopy()
}.keyboardShortcut(KeyEquivalent("c"), modifiers: .command)
Button("Paste") {
appDelegate.contentView.editPaste()
}.keyboardShortcut(KeyEquivalent("v"), modifiers: .command)
Button("Undo") {
appDelegate.contentView.undo()
}.keyboardShortcut(KeyEquivalent("z"), modifiers: .command)
Button("Redo") {
appDelegate.contentView.redo()
}.keyboardShortcut(KeyEquivalent("z"), modifiers: [.command, .shift])
Button("Bold") {
appDelegate.contentView.bold()
}.keyboardShortcut(KeyEquivalent("b"), modifiers: .command)
Button("Italic") {
appDelegate.contentView.italic()
}.keyboardShortcut(KeyEquivalent("i"), modifiers: .command)
Button("Select All") {
appDelegate.contentView.editSelectAll()
}.keyboardShortcut(KeyEquivalent("a"), modifiers: .command)
}
}
}
}
Swift 5 solution was presented in https://stackoverflow.com/a/58225397/3984522. However, there's a nice package, which does the job https://github.com/soffes/HotKey in a couple of lines of code:
import SwiftUI
import HotKey
#main
struct MenuBarPopoverApp: App {
#NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
Settings{
EmptyView()
}
.commands {
MenuBarPopoverCommands(appDelegate: appDelegate)
}
}
}
class AppDelegate: NSObject, NSApplicationDelegate, ObservableObject {
var popover = NSPopover.init()
var statusBarItem: NSStatusItem?
var contentView: ContentView!
let hotKey = HotKey(key: .x, modifiers: [.control, .shift]) // Global hotkey
override class func awakeFromNib() {}
func applicationDidFinishLaunching(_ notification: Notification) {
print("Application launched")
NSApplication.shared.activate(ignoringOtherApps: true)
contentView = ContentView()
popover.animates = false
popover.behavior = .transient
let contentVc = NSViewController()
contentVc.view = NSHostingView(rootView: contentView.environmentObject(self))
popover.contentViewController = contentVc
statusBarItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
let itemImage = NSImage(systemSymbolName: "eye", accessibilityDescription: "eye")
itemImage?.isTemplate = true
statusBarItem?.button?.image = itemImage
statusBarItem?.button?.action = #selector(AppDelegate.togglePopover(_:))
hotKey.keyUpHandler = { // Global hotkey handler
self.togglePopover()
}
}
#objc func showPopover(_ sender: AnyObject? = nil) {
if let button = statusBarItem?.button {
NSApplication.shared.activate(ignoringOtherApps: true)
popover.show(relativeTo: button.bounds, of: button, preferredEdge: NSRectEdge.minY)
}
}
#objc func closePopover(_ sender: AnyObject? = nil) {
popover.performClose(sender)
}
#objc func togglePopover(_ sender: AnyObject? = nil) {
if popover.isShown {
closePopover(sender)
} else {
showPopover(sender)
}
}
}

Binding with "RichTextField" (i.e. NSTextView in NSViewRepresentable) - Value resets after redraw, LostFocus, etc

I'm struggling with a custom made RichTextField, i.e. a NSTextView in a NSViewRepresentable, which has the below code (programmed with the help of How to use an NSAttributedString with a ScrollView in SwiftUI?):
Setting the attributedString in the code works and I can change the formatting, but as soon as the application loses the focus, the RichTextField resets to the last value set programmatically:
Furthermore, when using the RichTextField in a List, the application goes into a loop.
RichTextField
import Foundation
import SwiftUI
struct RichTextField: NSViewRepresentable {
typealias NSViewType = NSTextView
#Binding var attributedString: NSMutableAttributedString
var isEditable: Bool
func makeNSView(context: Context) -> NSTextView {
let textView = NSTextView(frame: .zero)
textView.textStorage?.setAttributedString(self.attributedString)
textView.isEditable = isEditable
textView.translatesAutoresizingMaskIntoConstraints = false
textView.autoresizingMask = [.width, .height]
return textView
}
func updateNSView(_ nsView: NSTextView, context: Context) {
nsView.textStorage?.setAttributedString(self.attributedString)
}
}
View
import SwiftUI
struct EditWindow: View {
#ObservedObject var model: EditEntryViewModel
var body: some View {
VStack (alignment: .leading, spacing: 20) {
RichTextField(attributedString: self.$model.answer1, isEditable: true)
.frame(maxWidth: .infinity, maxHeight: .infinity)
Spacer()
}.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity)
}
}
ViewModel
import Foundation
import SwiftUI
class EditEntryViewModel: ObservableObject {
init(entryID: Int32) {
let entryToUse = db.getEntry(id: entryID)
id = entryToUse!.id
answer1 = entryToUse!.answer1.getNSMutableAttributedStringFromHTML() // Converts HTML from the DB to a NSMutableAttributedString
}
#Published var id: Int32
#Published var answer1: NSMutableAttributedString = NSMutableAttributedString() {
didSet {
print("NEW answer1: " + answer1.string)
}
}
}
I wonder if there is a way to bind the attributedString to the ViewModel?
Thanks a lot for the help.
Not sure if it's the correct way to do it, but I got the binding to work with the following code:
import Foundation
import SwiftUI
struct RichTextField: NSViewRepresentable {
typealias NSViewType = NSTextView
#Binding var attributedString: NSAttributedString
var isEditable: Bool
func makeCoordinator() -> Coordinator {
Coordinator(self)
}
func makeNSView(context: Context) -> NSTextView {
let textView = NSTextView(frame: .zero)
textView.textStorage?.setAttributedString(self.attributedString)
textView.isEditable = isEditable
textView.delegate = context.coordinator
textView.translatesAutoresizingMaskIntoConstraints = false
textView.autoresizingMask = [.width, .height]
return textView
}
func updateNSView(_ nsView: NSTextView, context: Context) {
nsView.textStorage!.setAttributedString(self.attributedString)
}
// Source: https://medium.com/fantageek/use-xib-de9d8a295757
class Coordinator: NSObject, NSTextViewDelegate {
let parent: RichTextField
init(_ RichTextField: RichTextField) {
self.parent = RichTextField
}
func textDidChange(_ notification: Notification) {
guard let textView = notification.object as? NSTextView else { return }
self.parent.attributedString = textView.attributedString()
}
}
}
(I still get the "cycle detected" error when using the RichTextField in a List (non-editable) though..)

SwiftUI Wrapper for UITextView not updating ObservedObject

I hope I'm wrong, but I have not been able to find a SwiftUI equivalent to an editable
UITextView. So, I built one using UIViewRepresentable. Populating both a SwiftUI Text
and my own view with the ObservableObject works - but updates made in my view are
not propagated to the ObservableObject. I must be missing something important with
the Binding concept. Any guidance would be appreciated.
import SwiftUI
import Combine
struct ContentView: View {
#ObservedObject var myOText: MyOText
var body: some View {
ScrollView {
VStack {
Text("This is a bound Text View")
.padding(.top, 10)
.font(.headline)
Text(myOText.inTheCourse)
.lineLimit(3)
.padding()
Text("This is a multi-line UITextView wrapper:")
.font(.headline)
MultilineTextView(myOText: myOText)
.frame(height: 100)
.padding()
Spacer()
}
}
}
}
struct MultilineTextView: UIViewRepresentable {
#ObservedObject var myOText: MyOText
func makeUIView(context: Context) -> UITextView {
let view = UITextView()
view.isScrollEnabled = true
view.isEditable = true
view.isUserInteractionEnabled = true
view.textAlignment = .center
view.font = UIFont(name: "Times New Roman", size: 20)
return view
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = myOText.inTheCourse
}
}
class MyOText: ObservableObject {
#Published var inTheCourse: String = "When in the Course of human events, it becomes necessary for one people to dissolve the political bands which have connected them ..."
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(myOText: MyOText())
}
}
Xcode Version 11.2 beta 2 (11B44), iOS 13.
Your multiline text view needs a coordinator to observe the text updates from UITextView
struct MultilineTextView: UIViewRepresentable {
#ObservedObject var myOText: MyOText
func makeUIView(context: Context) -> UITextView {
let view = UITextView()
view.isScrollEnabled = true
view.isEditable = true
view.isUserInteractionEnabled = true
view.textAlignment = .center
view.font = UIFont(name: "Times New Roman", size: 20)
view.delegate = context.coordinator
return view
}
func updateUIView(_ uiView: UITextView, context: Context) {
uiView.text = myOText.inTheCourse
}
func makeCoordinator() -> MultilineTextView.Coordinator {
Coordinator(self)
}
class Coordinator: NSObject, UITextViewDelegate {
var control: MultilineTextView
init(_ control: MultilineTextView) {
self.control = control
}
func textViewDidChange(_ textView: UITextView) {
control.myOText.inTheCourse = textView.text
}
}
}

Resize image from internet

My app retrieves an image from URL, but I need to change the image size before it appears on the user interface in my tableview.
Here is my tableViewController code:
import UIKit
import Firebase
import Alamofire
class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var postField: MaterialTextField!
#IBOutlet weak var imageSelectorImage: UIImageView!
var posts = [Post]()
var imageSelected = false
var imagePicker: UIImagePickerController!
static var imageCache = NSCache()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
postField.delegate = self
//tableView.estimatedRowHeight = 400
//tableView.rowHeight = UITableViewAutomaticDimension
imagePicker = UIImagePickerController()
imagePicker.delegate = self
DataService.ds.REF_POSTS.queryOrderedByChild("timestamp").observeEventType(.Value, withBlock: { snapshot in
self.posts = []
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, dictionary: postDict)
self.posts.insert(post, atIndex: 0)
}
}
}
self.tableView.reloadData()
})
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
/**
* Called when the user click on the view (outside the UITextField).
*/
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 167)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 167)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
let movementDuration:NSTimeInterval = 0.1
let movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
print(post.postDescription)
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
if let url = post.imageUrl {
img = FeedViewController.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
return cell
} else {
return PostCell()
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
imageSelectorImage.image = image
imageSelected = true
}
#IBAction func selectImage(sender: UITapGestureRecognizer) {
presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func makePost(sender: AnyObject) {
//TODO: Add loading spinner while data is being processed
if let txt = postField.text where txt != "" {
if let img = imageSelectorImage.image where imageSelected == true {
let urlStr = URL_IMGSHACK
let url = NSURL(string: urlStr)!
//FIXME: Add error handling
let imgData = UIImageJPEGRepresentation(img, 0.2)!
let keyData = API_KEY_IMG.dataUsingEncoding(NSUTF8StringEncoding)!
let keyJSON = "json".dataUsingEncoding(NSUTF8StringEncoding)!
Alamofire.upload(.POST, url, multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imgData, name: "fileupload", fileName: "image",
mimeType: "image/jpg")
multipartFormData.appendBodyPart(data: keyData, name: "key")
multipartFormData.appendBodyPart(data: keyJSON, name: "format")
}) { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON(completionHandler: { response in
if let info = response.result.value as? Dictionary<String, AnyObject> {
if let links = info["links"] as? Dictionary<String, AnyObject>{
if let imgLink = links["image_link"] as? String {
print("LINK: \(imgLink)")
self.postToFirebase(imgLink)
}
}
}
})
case .Failure(let error):
print(error)
}
}
} else {
self.postToFirebase(nil)
}
}
}
func postToFirebase(imgUrl: String?) {
var post: Dictionary<String, AnyObject> = [
"timestamp": NSNumber(longLong: currentTimeMillis()),
"description": postField.text!,
"likes": 0
]
if imgUrl != nil {
post["imageUrl"] = imgUrl!
}
let firebasePost = DataService.ds.REF_POSTS.childByAutoId()
firebasePost.setValue(post)
postField.text = ""
imageSelectorImage.image = UIImage(named: "camera")
imageSelected = false
tableView.reloadData()
postField.resignFirstResponder()
}
func currentTimeMillis() ->Int64 {
let nowDouble = NSDate().timeIntervalSince1970
return Int64(nowDouble * 1000)
}
}
and here is my custom cell:
class PostCell: UITableViewCell {
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var showcaseImage: UIImageView!
#IBOutlet weak var descriptionText: UILabel!
#IBOutlet weak var likesLabel: UILabel!
#IBOutlet weak var likeImage: UIImageView!
var post: Post!
var request: Request?
var likeRef: Firebase!
override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action: "likeTapped:")
tap.numberOfTapsRequired = 1
likeImage.addGestureRecognizer(tap)
likeImage.userInteractionEnabled = true
}
override func drawRect(rect: CGRect) {
profileImage.layer.cornerRadius = self.profileImage.frame.size.width / 2
profileImage.backgroundColor = UIColor.clearColor()
profileImage.layer.borderWidth = 2
profileImage.layer.borderColor = UIColor.whiteColor().CGColor
self.profileImage.clipsToBounds = true
self.showcaseImage.clipsToBounds = true
}
func configureCell(post: Post, img: UIImage?){
self.post = post
likeRef = DataService.ds.REF_USERS_CURRENT.childByAppendingPath("likes").childByAppendingPath(post.postKey)
self.descriptionText.text = post.postDescription
self.likesLabel.text = "\(post.likes)"
if post.imageUrl != nil {
if img != nil {
self.showcaseImage.image = img
} else {
request = Alamofire.request(.GET, post.imageUrl!).validate(contentType: ["image/*"]).response(completionHandler: { request, response, data, err in
if err == nil {
let img = UIImage(data: data!)!
self.showcaseImage.image = img
FeedViewController.imageCache.setObject(img, forKey: self.post.imageUrl!)
} else {
print(err.debugDescription)
}
})
}
} else {
self.showcaseImage.hidden = true
}
likeRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let doesNotExist = snapshot.value as?
NSNull {
//This mean we have not liked this specific post
self.likeImage.image = UIImage(named: "heart-empty")
} else {
self.likeImage.image = UIImage(named: "heart-full")
}
})
}
func likeTapped(sender: UITapGestureRecognizer) {
likeRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let doesNotExist = snapshot.value as?
NSNull {
self.likeImage.image = UIImage(named: "heart-full")
self.post.adjustLikes(true)
self.likeRef.setValue(true)
} else {
self.likeImage.image = UIImage(named: "heart-empty")
self.post.adjustLikes(false)
self.likeRef.removeValue()
}
})
}
}
Here is an example showing what I want to achieve:
this when user post the portrait image
this when user post the landscape image
In summary, I want the image width to fit the width of the device screen, and the height of the uiimage to be dynamic, regardless of the image orientation (landscape or portrait).
Try this part, where 300x600 is needed size of your image to save.
let image = UIImage(data: data!)
UIGraphicsBeginImageContext(CGSizeMake(300, 600))
image?.drawInRect(CGRectMake(0, 0, 300, 600))
let smallImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

need help about resize image swift 2

My app retrieves image from URL, but I need to change the image size before it appears on the user interface in my tableview.
this is my code in my tableViewController:
import UIKit
import Firebase
import Alamofire
class FeedViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate {
#IBOutlet weak var tableView: UITableView!
#IBOutlet weak var postField: MaterialTextField!
#IBOutlet weak var imageSelectorImage: UIImageView!
var posts = [Post]()
var imageSelected = false
var imagePicker: UIImagePickerController!
static var imageCache = NSCache()
override func viewDidLoad() {
super.viewDidLoad()
tableView.delegate = self
tableView.dataSource = self
postField.delegate = self
//tableView.estimatedRowHeight = 400
//tableView.rowHeight = UITableViewAutomaticDimension
imagePicker = UIImagePickerController()
imagePicker.delegate = self
DataService.ds.REF_POSTS.queryOrderedByChild("timestamp").observeEventType(.Value, withBlock: { snapshot in
self.posts = []
if let snapshots = snapshot.children.allObjects as? [FDataSnapshot] {
for snap in snapshots {
if let postDict = snap.value as? Dictionary<String, AnyObject> {
let key = snap.key
let post = Post(postKey: key, dictionary: postDict)
self.posts.insert(post, atIndex: 0)
}
}
}
self.tableView.reloadData()
})
}
func textFieldShouldReturn(textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
/**
* Called when the user click on the view (outside the UITextField).
*/
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
self.view.endEditing(true)
}
func textFieldDidBeginEditing(textField: UITextField) {
animateViewMoving(true, moveValue: 167)
}
func textFieldDidEndEditing(textField: UITextField) {
animateViewMoving(false, moveValue: 167)
}
func animateViewMoving (up:Bool, moveValue :CGFloat){
let movementDuration:NSTimeInterval = 0.1
let movement:CGFloat = ( up ? -moveValue : moveValue)
UIView.beginAnimations( "animateView", context: nil)
UIView.setAnimationBeginsFromCurrentState(true)
UIView.setAnimationDuration(movementDuration )
self.view.frame = CGRectOffset(self.view.frame, 0, movement)
UIView.commitAnimations()
}
func numberOfSectionsInTableView(tableView: UITableView) -> Int {
return 1
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let post = posts[indexPath.row]
print(post.postDescription)
if let cell = tableView.dequeueReusableCellWithIdentifier("PostCell") as? PostCell {
cell.request?.cancel()
var img: UIImage?
if let url = post.imageUrl {
img = FeedViewController.imageCache.objectForKey(url) as? UIImage
}
cell.configureCell(post, img: img)
return cell
} else {
return PostCell()
}
}
func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
imagePicker.dismissViewControllerAnimated(true, completion: nil)
imageSelectorImage.image = image
imageSelected = true
}
#IBAction func selectImage(sender: UITapGestureRecognizer) {
presentViewController(imagePicker, animated: true, completion: nil)
}
#IBAction func makePost(sender: AnyObject) {
//TODO: Add loading spinner while data is being processed
if let txt = postField.text where txt != "" {
if let img = imageSelectorImage.image where imageSelected == true {
let urlStr = URL_IMGSHACK
let url = NSURL(string: urlStr)!
//FIXME: Add error handling
let imgData = UIImageJPEGRepresentation(img, 0.2)!
let keyData = API_KEY_IMG.dataUsingEncoding(NSUTF8StringEncoding)!
let keyJSON = "json".dataUsingEncoding(NSUTF8StringEncoding)!
Alamofire.upload(.POST, url, multipartFormData: { multipartFormData in
multipartFormData.appendBodyPart(data: imgData, name: "fileupload", fileName: "image",
mimeType: "image/jpg")
multipartFormData.appendBodyPart(data: keyData, name: "key")
multipartFormData.appendBodyPart(data: keyJSON, name: "format")
}) { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
upload.responseJSON(completionHandler: { response in
if let info = response.result.value as? Dictionary<String, AnyObject> {
if let links = info["links"] as? Dictionary<String, AnyObject>{
if let imgLink = links["image_link"] as? String {
print("LINK: \(imgLink)")
self.postToFirebase(imgLink)
}
}
}
})
case .Failure(let error):
print(error)
}
}
} else {
self.postToFirebase(nil)
}
}
}
func postToFirebase(imgUrl: String?) {
var post: Dictionary<String, AnyObject> = [
"timestamp": NSNumber(longLong: currentTimeMillis()),
"description": postField.text!,
"likes": 0
]
if imgUrl != nil {
post["imageUrl"] = imgUrl!
}
let firebasePost = DataService.ds.REF_POSTS.childByAutoId()
firebasePost.setValue(post)
postField.text = ""
imageSelectorImage.image = UIImage(named: "camera")
imageSelected = false
tableView.reloadData()
postField.resignFirstResponder()
}
func currentTimeMillis() ->Int64 {
let nowDouble = NSDate().timeIntervalSince1970
return Int64(nowDouble * 1000)
}
}
and here my custom cell:
class PostCell: UITableViewCell {
#IBOutlet weak var profileImage: UIImageView!
#IBOutlet weak var showcaseImage: UIImageView!
#IBOutlet weak var descriptionText: UILabel!
#IBOutlet weak var likesLabel: UILabel!
#IBOutlet weak var likeImage: UIImageView!
var post: Post!
var request: Request?
var likeRef: Firebase!
override func awakeFromNib() {
super.awakeFromNib()
let tap = UITapGestureRecognizer(target: self, action: "likeTapped:")
tap.numberOfTapsRequired = 1
likeImage.addGestureRecognizer(tap)
likeImage.userInteractionEnabled = true
}
override func drawRect(rect: CGRect) {
profileImage.layer.cornerRadius = self.profileImage.frame.size.width / 2
profileImage.backgroundColor = UIColor.clearColor()
profileImage.layer.borderWidth = 2
profileImage.layer.borderColor = UIColor.whiteColor().CGColor
self.profileImage.clipsToBounds = true
self.showcaseImage.clipsToBounds = true
}
func configureCell(post: Post, img: UIImage?){
self.post = post
likeRef = DataService.ds.REF_USERS_CURRENT.childByAppendingPath("likes").childByAppendingPath(post.postKey)
self.descriptionText.text = post.postDescription
self.likesLabel.text = "\(post.likes)"
if post.imageUrl != nil {
if img != nil {
self.showcaseImage.image = img
} else {
request = Alamofire.request(.GET, post.imageUrl!).validate(contentType: ["image/*"]).response(completionHandler: { request, response, data, err in
if err == nil {
let img = UIImage(data: data!)!
self.showcaseImage.image = img
FeedViewController.imageCache.setObject(img, forKey: self.post.imageUrl!)
} else {
print(err.debugDescription)
}
})
}
} else {
self.showcaseImage.hidden = true
}
likeRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let doesNotExist = snapshot.value as?
NSNull {
//This mean we have not liked this specific post
self.likeImage.image = UIImage(named: "heart-empty")
} else {
self.likeImage.image = UIImage(named: "heart-full")
}
})
}
func likeTapped(sender: UITapGestureRecognizer) {
likeRef.observeSingleEventOfType(.Value, withBlock: { snapshot in
if let doesNotExist = snapshot.value as?
NSNull {
self.likeImage.image = UIImage(named: "heart-full")
self.post.adjustLikes(true)
self.likeRef.setValue(true)
} else {
self.likeImage.image = UIImage(named: "heart-empty")
self.post.adjustLikes(false)
self.likeRef.removeValue()
}
})
}
}
this example for what i want to achieve:
this when user post the portrait image
this when user post the landscape image
The point is that I want my image width to always fit the width of device screen, and the height of the uiimage will be dynamic.
so when user post any image with any orientation ( landscape or portrait ), the image will fit the width of the screen, the height will be dynamic.
please help me give the detail where to put your method since i am a newbie about this, how to achieve what i want... im so desperated, is already 1 month and im so stuck about this...
You need to add imageview to storyboard from interface builder in Xcode,then you can set its constraints to fit size of the screen,& also add height constraints which you can change later when you retrieve image from url

Resources