Accessing a function inside a class function in Swift - ios

How would I call darkness and put parameters in it?
class imageProcesses {
class func DefaultProcesses() {
func CompleteDarkness(image: UIImage) -> UIImage {
let dark = Darken(124, image: image)
return dark
}
}
}
iv'e tried this:
ImageProcessor.DefaultProcesses().CompleteDarkness(image!)
but it doesn't work

You can't call a nested function from outside. Move it out to a class function:
class imageProcesses {
class func CompleteDarkness(image: UIImage) -> UIImage {
let dark = Darken(124, image: image)
return dark
}
class func DefaultProcesses() {
// ...
}
}

Why are you declaring a function inside of a function anyway?
class imageProcesses {
class func DefaultProcesses(image: UIImage) {
completeDarkness(image: image)//calls completeDarkness.
}
func CompleteDarkness(image: UIImage) -> UIImage {
let dark = Darken(124, image: image)
return dark
}
}
Later call
ImageProcessor.DefaultProcesses(image: image!)//Now calling DefaultProcesses will run Complete Darkness and anything else you put in there.

Think this is what you want you can stick that in the play ground and have a go with it
func Darken(input:Int, image:UIImage) -> UIImage{
return UIImage()
}
class ImageProcesses {
class func DefaultProcesses() -> ((UIImage) -> UIImage){
func CompleteDarkness(image: UIImage) -> UIImage {
let dark = Darken(124, image: image)
return dark
}
return CompleteDarkness
}
}
let image = UIImage()
ImageProcesses.DefaultProcesses()(image)

Related

Mesibo Contact placeholder issue ios swift

I have intregrated mesibo chat sdk in flutter framework all is working good but can't able to have contact letter image after enabling the useLetterTitleImage = true . Can any body can tell what function add for contact image android it is working but getting issue on ios i am sharig file and screen shot as well
Code :
import UIKit
import mesibo
import MesiboUI
class SecondViewController: UIViewController,MesiboDelegate {
override func viewDidLoad() {
super.viewDidLoad()
let ui = Mesibo.getInstance().getUiOptions()
UINavigationBar.appearance().tintColor = UIColor.white
ui?.contactPlaceHolder = nil
ui?.mStatusBarColor = 0xf000000
ui?.mToolbarColor = 0xf000000
ui?.enableBackButton = false
ui?.useLetterTitleImage = true
ui?.emptyUserListMessage = "NO MESSAGES"
Mesibo.getInstance()?.setUiOptions(ui!)
navigationController?.navigationItem.setHidesBackButton(true, animated:false)
navigationController?.pushViewController(MesiboUI.getViewController(navigationController?.delegate), animated: false)
Mesibo.getInstance()?.addListener(self)
}
override func didMove(toParent parent: UIViewController?) {
super.didMove(toParent: parent)
if parent == nil {
debugPrint("Back Button pressed.")
}
}
private func add(asChildViewController viewController: UIViewController) {
// Add Child View Controller
addChild(viewController)
// Add Child View as Subview
view.addSubview(viewController.view)
// Configure Child View
viewController.view.frame = view.bounds
viewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
// Notify Child View Controller
viewController.didMove(toParent: self)
}
func mesibo_(onConnectionStatus status: Int32) {
print("Connection status: %d", status);
}
func mesibo_(onMessageStatus params: MesiboParams!) {
print(params ?? "")
}
func mesibo_(onMessage params: MesiboParams!, data: Data!) {
print("data")
}
func mesibo_(onMessageFilter params: MesiboParams!, direction: Int32, data: Data!) -> Bool {
return true
}
func mesibo_(onUserProfileUpdated profile: MesiboUserProfile!, action: Int32, refresh: Bool) {
}
func mesibo_(onShowProfile parent: Any!, profile: MesiboUserProfile!) {
}
func mesibo_(onUpdateUserProfiles profile: MesiboUserProfile!) -> Bool {
return true
}
You should set a profile picture for the mesibo UserProfile. If not, it will use a letter titler image
This is the code https://github.com/mesibo/ui-modules-ios/blob/master/Messaging/Messaging/UserListViewController.m#L1149
As you can see, it sets a letter titler image if it can't find the thumbnail.

Custom UIActivityViewController not appear image when it is in the "More" menu item

I created custom UIActivity and override variable activityImage used my image.
fileprivate extension UIActivity.ActivityType {
static let extendedMessage =
UIActivity.ActivityType("ExtendedMessage")
}
Custom UIActivity:
fileprivate class ExtendedMessageActivity: UIActivity {
private let phoneNumbers: [String]
private var message: String?
init(phoneNumbers: [String]) {
self.phoneNumbers = phoneNumbers
super.init()
}
override static var activityCategory: UIActivity.Category {
return .share
}
override var activityType: UIActivity.ActivityType? { return .extendedMessage }
override var activityTitle: String? { return NSLocalizedString("Message", comment: "") }
override var activityImage: UIImage? {
return UIImage(named: "message-app-icon")
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
....
}
override func prepare(withActivityItems activityItems: [Any]) {
....
}
override func perform() {
....
}
}
The image appears in the set.
I have a problem the image does not appear when it is in the "More" menu item.
Why the image does not appear?
Had tried to find documentation for the activity settings icon. Not found proper documentation. Just add the below code in your custom UIActivity, Working fine for me with Swift 5.2
#objc var _activitySettingsImage: UIImage? {
return UIImage(named: "activitySettingsIcon")
}
Don't forgot to activitySettingsIcon image set in 29pt.
I solved the same issue by adding the following (in Objective C, but you get the idea).
- (UIImage *)_activitySettingsImage {
return [UIImage imageNamed:#"message-app-icon-for-setting"];
}
Apparently, image for more section should be smaller than the one in _activityImage. At the time of writing (XCode 12.3), 60pt for activityImage and 30pt for activitySettingsImage work for me. (I also added #2x for each image)..

How to send image from framework to sample project in iOS Swift?

I have implemented document scanner using visionKit. Initially i have faced camera dismiss issue and it fixed. Now i am trying to send image after camera dismiss from framework to sample project.
I have tried using completion handler, but it does not work.
Here is the code for framework:
public class A8Scan: NSObject, VNDocumentCameraViewControllerDelegate {
var imageNew: UIImage?
var statusImage: UIImageView?
private var clientView: UIViewController?
public init(_ viewController:UIViewController){
self.clientView = viewController
}
public func showScanner(imgData: UIImage?){
self.createTaskController(img: imgData)
print("Called Build")
}
private func createTaskController(img: UIImage?){
let scannerViewController = VNDocumentCameraViewController()
scannerViewController.delegate = self
self.clientView?.present(scannerViewController,animated:true,completion: {
self.imageNew = img
})
}
public func imageFromFile(result: #escaping (_ image: UIImage?) -> Void){
//the image
let scannerViewController = VNDocumentCameraViewController()
scannerViewController.delegate = self
self.clientView?.present(scannerViewController,animated:true,completion: nil)
if imageNew != nil {
result(imageNew)
}
else{
//callback nil so the app does not pause infinitely if
//the error != nil
result(nil)
}
}
func set(image: UIImage) {
self.statusImage?.image = image
}
public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
guard scan.pageCount >= 1 else {
controller.dismiss(animated: true)
return
}
let originalImage = scan.imageOfPage(at: 0)
let newImage = compressedImage(originalImage)
imageNew = newImage
set(image: imageNew!)
print("new image::\(newImage.size)")
controller.dismiss(animated: true, completion: nil)
// processImage(newImage)
}
public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFailWithError error: Error) {
print(error)
controller.dismiss(animated: true)
}
public func documentCameraViewControllerDidCancel(_ controller: VNDocumentCameraViewController) {
controller.dismiss(animated: true)
}
func compressedImage(_ originalImage: UIImage) -> UIImage {
guard let imageData = originalImage.jpegData(compressionQuality: 1),
let reloadedImage = UIImage(data: imageData) else {
return originalImage
}
return reloadedImage
}
}
Here is the code sample project:
#IBAction func btnAction(_ sender: Any) {
// A8Scan(self).showScanner()
// A8Scan(self).showScanner(imgData: im)
// print("nn", A8Scan(self).showScanner(imgData: im))
A8Scan(self).imageFromFile { (image) in
if image != nil {
print(image!)
} else {
print("something went wrong")
}
}
// p()
}
func p (){
ScannerViewController().imageFromFile{(image: UIImage?) -> Void in
//use the image that was just retrieved
print("image data", image)
}
}
My issue is after camera dismiss from framework it does not send image from framework to sample project.
Any help much appreciated pls....
1-
Add this inside your framework
var callBack((UIImage)-())?
public func show() {
let scannerViewController = VNDocumentCameraViewController()
scannerViewController.delegate = self
self.clientView?.present(scannerViewController,animated:true,completion: nil)
}
2-
let newImage = compressedImage(originalImage)
callBack?(newImage)
3-
Then inside the vc that uses it
let vc = A8Scan(self)
vc.show()
vc.callBack = { [weak self] image in
}
As you are using a middle man (A8Scan) NSObject to present a controller. the A8Scan will get deallocated immediately after presenting the controller which you can confirm by keeping a deinit function inside A8Scan.
A workaround for this is to make your NSObject (A8Scan) as a singleton ie add
static let shared = A8Scan()
var callBack: ((_ image: UIImage?) -> Void)?
remove the existing init method and add the viewcontroller property into the imageFromFile: method, your function will look like this
public func imageFromFile(sender: UIViewController, result: #escaping (_ image: UIImage?) -> Void){
let scannerViewController = VNDocumentCameraViewController()
scannerViewController.delegate = self
self.callBack = result
sender.present(scannerViewController,animated:true,completion: nil)
}
public func documentCameraViewController(_ controller: VNDocumentCameraViewController, didFinishWith scan: VNDocumentCameraScan) {
guard scan.pageCount >= 1 else {
controller.dismiss(animated: true)
return
}
let originalImage = scan.imageOfPage(at: 0)
let newImage = compressedImage(originalImage)
imageNew = newImage
set(image: imageNew!)
callBack?(imageNew!)
controller.dismiss(animated: true, completion: nil)
}
and finally in your sample project,
#IBAction func btnAction(_ sender: Any) {
A8Scan.shared.imageFromFile(sender: self) { (image) in
if image != nil {
print(image!)
} else {
print("something went wrong")
}
}
}

how to test methods which set private variables in viewcontroller unit testing?

I have a ViewController, which conforms to a protocol. ViewController has the following private UI components:
backgroundImage, logoimage, loginButton , signupButton - all of these are private. (hence not accessible by unit tests)
How can I test the below protocol method implementation in unit testing? I am using XCTestFramework for unit testing.
extension ViewController : ViewControllerProtocol{
func setbackgroundImage(_ image: UIImage) {
backgroundImage.image = image
}
func setLogoImage(_ image: UIImage) {
logoImage.image = image
}
func setLoginButtontitle(_ title: String) {
loginButton.setTitle(title, for: .normal)
}
func setSignupButtonTitle(_ title: String) {
signupButton.setTitle(title, for: .normal)
}
}
protocol ViewControllerProtocol {
func setbackgroundImage(_ image : UIImage)
func setLogoImage(_ image : UIImage)
func setLoginButtontitle(_ title: String)
func setSignupButtonTitle(_ title: String)
func setTitleLabel(_ title: String)
func setContinuewithoutsignupTitle(_ title: String)
}
You can still unit test those values below by adding public getters
protocol ViewControllerProtocol {
func setbackgroundImage(_ image : UIImage)
func setLogoImage(_ image : UIImage)
func setLoginButtontitle(_ title: String)
func setSignupButtonTitle(_ title: String)
func setTitleLabel(_ title: String)
func setContinuewithoutsignupTitle(_ title: String)
func getbackgroundImage() -> UIImage?
func getlogoImage() -> UIImage?
func getloginButtonTitle() -> String?
func getsignupButtonTitle() -> String?
}
class MainTest: XCTestCase {
let viewController = ViewController()
override func setUp() {
super.setUp()
}
func testExample() {
guard let testImage1 = UIImage(named: "testImage1") else {
XCTFail("Could not load test image 1")
return
}
viewController.setbackgroundImage(testImage1)
XCTAssert(viewController.getbackgroundImage() == testImage1)
guard let testImage2 = UIImage(named: "testImage2") else {
XCTFail("Could not load test image 2")
return
}
viewController.setLogoImage(testImage2)
XCTAssert(viewController.getlogoImage() == testImage2)
viewController.setLoginButtontitle("test")
XCTAssert(viewController.getloginButtonTitle() == "test")
viewController.setSignupButtonTitle("test")
XCTAssert(viewController.getsignupButtonTitle() == "test")
}
}

How to print SFSafariViewController full web page?

I am opening a pdf file in SFSafariViewController. Now I want to print this PDF from SFSafariViewController.
But Share button of safari view controller does not have print option.
Please tell me how to add print functionality to SFSafariViewController.
You can custom activity.
extension ViewController: SFSafariViewControllerDelegate {
func safariViewController(_ controller: SFSafariViewController, activityItemsFor URL: URL, title: String?) -> [UIActivity] {
let acitivity = CustomActivity()
return [acitivity]
}
func printURL(url: URL) {
}
}
CustomActivity.swift
import Foundation
import UIKit
class CustomActivity: UIActivity {
override class var activityCategory: UIActivityCategory {
return .action
}
override var activityType: UIActivityType? {
return UIActivityType.print
}
override var activityTitle: String? {
return "Print"
}
override var activityImage: UIImage? {
return nil
}
override func canPerform(withActivityItems activityItems: [Any]) -> Bool {
return true
}
override func perform() {
// do something here
}
}

Resources