Difficulty loading Image in Swift with Kingfisher - ios

I have a strange problem. I am trying to use Kingfisher in order to load and an cache an Image from Firebase in my app. The problem is that kKingfisher does not download the image. I am using a Placeholder Image that is locally stored and the scroll view displays that Image. If I remove the placeholder part from the command, the app crashes. So I know the Kingfisher function works by at least placing the placeholder Image into the UIImageView but not the image from the URL. Here is the code:
Can you point me to the right direction?
import UIKit
import Kingfisher
class Backprogramme: UIViewController {
override var prefersStatusBarHidden: Bool {
return true
}
#IBOutlet var BackPScroll: UIScrollView!
var imageArray = [UIImage]()
var folie1Image = UIImageView()
override func viewDidLoad() {
super.viewDidLoad()
let folie1URL = URL(string: "https://firebasestorage.googleapis.com/v0/b/backmaster-cdb60.appspot.com/o/Folie1.PNG?alt=media&token=efcb8e93-b817-41f3-a96f-946fd47cf468")!
folie1Image.kf.setImage(with: folie1URL, placeholder: #imageLiteral(resourceName: "first"))
imageArray = [folie1Image.image!]
for i in 0..<imageArray.count {
let imageView = UIImageView()
imageView.image = imageArray[i]
let xPosition = self.view.frame.width * CGFloat(i)
imageView.frame = CGRect(x: xPosition, y: 0, width: self.BackPScroll.frame.width, height: self.BackPScroll.frame.height)
BackPScroll.contentSize.width = BackPScroll.frame.width * CGFloat(i+1)
BackPScroll.addSubview(imageView)
}
// Do any additional setup after loading the view.
}
}

I had the same issue , discovered I had this in the log "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection" .
I fixed it by adding NSAppTransportSecurity as a Dictionary in the info.plist and then I added Allow Arbitrary Loads boolean with YES as a value . It worked instantly.

Related

Nothing is showing when using BCMeshTransformView

I wanted to use BCMeshTransformView library into my swift project. I've created an empty swift project and added the library via cocoa pods.
Here's my ViewController class:
import UIKit
import BCMeshTransformView
class ViewController: UIViewController {
var transformView:BCMeshTransformView!
var imageView:UIImageView!
var transform:BCMutableMeshTransform!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
transformView = BCMeshTransformView(frame: self.view.bounds)
transformView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
imageView = UIImageView(image: UIImage(named: "picture.jpg")!)
imageView.center = CGPoint(x: transformView.contentView.bounds.midX, y: transformView.contentView.bounds.midY)
transformView.contentView.addSubview(imageView)
transformView.diffuseLightFactor = 0.0
transform = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: 20, numberOfColumns: 20)
transform.mapVertices { (vertex, vertexIndex) -> BCMeshVertex in
return BCMeshVertex(from: vertex.from, to: vertex.to)
}
transformView.meshTransform = transform
self.view.addSubview(transformView)
}
}
When I'm running the app nothing is showing. It's entirely white.
Removing this code:
transform = BCMutableMeshTransform.identityMeshTransform(withNumberOfRows: 20, numberOfColumns: 20)
transform.mapVertices { (vertex, vertexIndex) -> BCMeshVertex in
return BCMeshVertex(from: vertex.from, to: vertex.to)
}
transformView.meshTransform = transform
doesn't change anything.
But when in xcode I'm switching to "Show UI hierarchy" I can see an image:
Here's a whole sample project:
http://www116.zippyshare.com/v/IUTXbKJg/file.html
Why I cannot see anything? I tried making an example as simple as possible.
Looks like that was a problem with that library codun't find shader files. I had to copy BCMeshShader.fsh and BCMeshShader.vsh to my project.

ScaleAspectFit blank spaces, imageView.image nul

I have a UIImageView, where the image is set with a given url. Then, I set the content mode to Scale Aspect Fit. This works fine, but there is a ton of blank space before and after the image, when the image is supposed to be directly at the top of the screen.
What I would like to do is rescale the UIImage size (maybe frame?) to match the new size created when Aspect Fit is applied (seems to be the suggestion most people received).
The problem is, whenever I test previous solutions, I'm getting a nul error. Particularly:
import UIKit
import AVFoundation
class OneItemViewController: UIViewController {
#IBOutlet weak var itemImage: UIImageView!
#IBOutlet weak var menuButton: UIBarButtonItem!
#IBOutlet weak var titleText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
let imageURL:NSURL? = NSURL(string: "https://upload.wikimedia.org/wikipedia/commons/d/d5/Pic_de_neige_cordier_Face_E.jpg")
if imageURL != nil {
itemImage.sd_setImageWithURL(imageURL)
itemImage.contentMode = UIViewContentMode.ScaleAspectFit
AVMakeRectWithAspectRatioInsideRect(itemImage.image!.size, itemImage.bounds)
/**
let imageSize:CGSize = onScreenPointSizeOfImageInImageView(itemImage)
var imageViewRect:CGRect = itemImage.frame
imageViewRect.size = imageSize
itemImage.frame = imageViewRect
**/
}
if self.revealViewController() != nil {
menuButton.target = self.revealViewController()
menuButton.action = "revealToggle:"
self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
}
self.titleText.text = "Title: " + "Earl and Countess of Derby with Edward, their Infant Son, and Chaplain"
// Do any additional setup after loading the view.
}
/**
func onScreenPointSizeOfImageInImageView(imageV: UIImageView) -> CGSize {
var scale: CGFloat
if (imageV.frame.size.width > imageV.frame.size.height) {
if (imageV.image!.size.width > imageV.image!.size.height) {
scale = imageV.image!.size.height / imageV.frame.size.height
} else {
scale = imageV.image!.size.width / imageV.frame.size.width
}
} else {
if (imageV.image!.size.width > imageV.image!.size.height) {
scale = imageV.image!.size.width / imageV.frame.size.width
} else {
scale = imageV.image!.size.height / imageV.frame.size.height
}
}
return CGSizeMake(imageV.image!.size.width / scale, imageV.image!.size.height / scale)
}
**/
}
Tried two things here to get rid of blank space.
First attempt is the call to AVMakeRectWithAspectRatioInsideRect.
Second attempt is the two chunks of code in the /** **/ comments. (onScreenPointSizeOfImageInImageView function and calls to it in viewDidLoad.)
But I can't tell if either work because itemImage.image!.size is causing an error.
So two questions:
1) Why is itemImage.image!.size giving me a nil while unwrapping?
2) Has anyone found a faster solution to removing blank spaces caused by AspectFit?
imageView.widthAnchor.constraint(equalTo: imageView.heightAnchor, multiplier: image.size.width / image.size.height).isActive = true
This answer is answered programmatically with UIKit with Swift 5
As mentioned by #Ignelio, using NSLayoutConstraint would do the work for UIImageView.
The reasoning is that you want to keep maintain the aspect ratio - by using
// let UIImage be whatever you decide to name it
UIImage.contentMode = .scaleAspectFit
would make the UIImage inside the UIImageView fit back to its ratio size given the width. However, as mentioned in Apple's documentation, that will leave remaining area with transparent spacing. Hence, what you want to tackle is UIImageView's size/frame.
--
With this method, you're giving your UIImageView its width constraint equal to the UIImage's ratio - scaling back perfectly in regards to its parent's width constraint (whatever that may be).
// let UIImageView be whatever you name
UIImageView.widthAnchor.constraint(equalTo: UIImageView.heightAnchor, multiplier: UIImage.size.width / UIImage.size.height).isActive = true

How Can I Add a Background Image for FirebaseUI Login for iOS?

I'd like to be able to place a background image behind the three sign-in buttons (for Google, Facebook, and Email) of the FirebaseUI login screen. FirebaseUI login is a drop-in authentication solution for iOS, Android, and Web. I'm having trouble with iOS.
There's a little bit of advice on Github, but not enough.
I first initialize my var customAuthPickerViewController : FIRAuthPickerViewController! near the top of the ViewController.swift file.
Then, this is the function in my ViewController.swift file, but it's not working. When I click the logout button, the app crashes, and no background image is ever shown.
// Customize the sign-in screen to have the Bizzy Books icon/background image
func authPickerViewController(for authUI: FIRAuthUI) -> FIRAuthPickerViewController {
customAuthPickerViewController = authPickerViewController(for: authUI)
backgroundImage = UIImageView(image: UIImage(named: "bizzybooksbee"))
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
customAuthPickerViewController.view.addSubview(backgroundImage)
return customAuthPickerViewController
}
The background image "bizzybooksbee" is a Universal Image Set with 1x, 2x, and 3x images already loaded in my Assets.xcassets folder.
Here's a picture of what the login screen looks like without trying to implement the background image.
UPDATE: I'm able to get the image to show, with the code I gave in the comments below, but it shows OVER the sign-in buttons, as in the pic below.
Here's an image of the "heirarchy," with Jeffrey's help:
Here's the solution!
Remove the junk that doesn't work from ViewController.swift:
func authPickerViewController(for authUI: FIRAuthUI) -> FIRAuthPickerViewController {
customAuthPickerViewController = authPickerViewController(for: authUI)
backgroundImage = UIImageView(image: UIImage(named: "bizzybooksbee"))
backgroundImage.contentMode = UIViewContentMode.scaleAspectFill
customAuthPickerViewController.view.addSubview(backgroundImage)
return customAuthPickerViewController
}
Create a .swift "subclass" of FIRAuthPickerViewController that you can name anything, and add your background image/customization there:
import UIKit
import FirebaseAuthUI
class BizzyAuthViewController: FIRAuthPickerViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
let width = UIScreen.main.bounds.size.width
let height = UIScreen.main.bounds.size.height
let imageViewBackground = UIImageView(frame: CGRect(x: 0, y: 0, width: width, height: height))
imageViewBackground.image = UIImage(named: "bizzybooksbee")
// you can change the content mode:
imageViewBackground.contentMode = UIViewContentMode.scaleAspectFill
view.insertSubview(imageViewBackground, at: 0)
}}
In your ViewController.swift (or whatever you call it), in the section where you login, change authViewController to equal your new subclass, add a line for the navigation controller, and pass it into the self.present:
let authViewController = BizzyAuthViewController(authUI: authUI!)
let navc = UINavigationController(rootViewController: authViewController)
self.present(navc, animated: true, completion: nil)
I also made a YouTube tutorial which shows how to do this in depth.

Preview UIWebView in iCarousel in Swift

I am writing a app in Swift where I am trying to load a preview of images using iCarousel of html pages for which I am using UIWebView.
Now the problem I have is I am unable to preview html pages, however when I trying to preview images, I am able to preview it.
Here is my code
func carousel(carousel: iCarousel, viewForItemAtIndex index: Int, reusingView view: UIView?) -> UIView {
var webView : UIImageView!
let webV:UIWebView = UIWebView(frame: CGRectMake(0, 0, 200, 200))
webV.loadRequest(NSURLRequest(URL: NSURL(string: "http://www.google.com")!))
webV.contentMode = .ScaleAspectFit
if webView == nil{
webView = UIImageView(frame:CGRect(x:0,y:0,width:200,height:200))
webView.contentMode = .ScaleAspectFit
}else{
webView = view as! UIImageView
}
webView.image = UIImage(named:"left-arrow.png")!
return webView //returns image carousel
//return webV //returns blank carousel. I want this to work !!!
}
How do I achieve this functionality ?
Edit 1
I can preview images for
carousel.type = iCarouselType.Linear screen attached.
This is how it looks
but I want it for any other carousel, say
carousel.type = iCarouselType.CoverFlow this is what I get
This is what I get when I am returning UIImageView
I have achieved this functionality by using WKWebView instead of UIWebView
Here is the working code
let webV:WKWebView!
webV = WKWebView(frame: CGRectMake(20, 20, 200, 200))
var url = NSURL(string:"http://www.fb.com/")
var req = NSURLRequest(URL:url!)
webV!.loadRequest(req)
return webV
Also,make sure you have imported WebKit by adding the below line
import WebKit

Trouble with UITableViewCell for MoPub Native Ad

I integrated MoPub advertising for my iOS app to run some native ads in my UITableViewController (actually, it's a PFQueryTableViewController because I'm using Parse to load data into the tableView).
Everything is running fine - the console shows that ads are being loaded successfully, and I can see the extra cell in my tableView where the ad should be - the problem is that none of the content for the ad is showing up; I only see a blank cell.
The problem doesn't seem to be from MoPub. I think I'm setting up my cell incorrectly because I can't get ANYTHING to show up there.
I have tried 2 things.
Setting up the cell using storyboard. I added a new cell to my UITableViewController and created a new class for it, NativeAdCell. I designed IBOutlets and connected them to the class file.
The content is loaded through a MoPub function like so:
func layoutAdAssets(adObject: MPNativeAd!) {
adObject.loadIconIntoImageView(iconImageView)
adObject.loadTitleIntoLabel(titleLabel)
adObject.loadCallToActionTextIntoButton(callToActionButton)
adObject.loadImageIntoImageView(mainImageView)
adObject.loadTextIntoLabel(mainTextLabel)
}
The app crashes at the first line with console output (lldb).
Adding the outlets programmatically. In the NativeAdCell file I add outlets like so:
var mainTextLabel = UILabel()
var titleLabel = UILabel()
var iconImageView = UIImageView()
var mainImageView = UIImageView()
var callToActionButton = UIButton()
And later setting their frames.
The content is loaded with the same MoPub function. Now there is no crash, but the ad cell shows up completely blank with no content. Even if I manually set mainTextLabel.text = "Please show up" nothing will show up.
The crash when I try to load the IBOutlets makes me think something is wrong with the way I'm linking up the NativeAdCell to the tableView, but it seems like the MoPub SDK should handle this.
Can anybody spot the problem based on this info?
I achieved this without IBOutlets as follows, it's not well documented so I figured this might help someone out who is looking to use the MoPub SDK with custom cells for native ads
//
// AdTableViewCell.swift
// Trending-News
//
// Created by Conor Griffin on 22/07/2015.
// Copyright (c) 2015 Conor Griffin. All rights reserved.
//
import UIKit
import MoPub
class AdTableViewCell: UITableViewCell, MPNativeAdRendering {
var iconImageView: UIImageView! = UIImageView()
var titleLabel: UILabel! = UILabel()
var mainTextLabel: UILabel! = UILabel()
var advertIndicator: CGSponsoredLinkLabel! = CGSponsoredLinkLabel()
var adCorner: CGCornerHighlight! = CGCornerHighlight()
static func sizeWithMaximumWidth(maximumWidth: CGFloat) -> CGSize {
return CGSize(width: maximumWidth, height: 100)
}
override func layoutSubviews() {
super.layoutSubviews()
let width = CGRectGetWidth(frame)
let cellFrame = CGRect(x: 0, y: 0, width: width, height: 100)
iconImageView.layer.borderColor = UIColor.grayColor().CGColor
iconImageView.layer.borderWidth = 1
iconImageView.frame = CGRectMake(10, 10, 80, 80)
contentView.addSubview(iconImageView)
titleLabel.frame = CGRectMake(100, 10, self.frame.width - 110, 25)
contentView.addSubview(titleLabel)
mainTextLabel.frame = CGRectMake(100, 35, self.frame.width - 110, self.frame.height - 65)
contentView.addSubview(mainTextLabel)
mainTextLabel.numberOfLines = 3
mainTextLabel.font = UIFont.systemFontOfSize(11.0)
advertIndicator.frame = CGRectMake(100, self.frame.height - 30, self.frame.width - 110, self.frame.height - 80)
contentView.addSubview(advertIndicator)
advertIndicator.font = UIFont.systemFontOfSize(10.0)
advertIndicator.text = "Advertisement"
advertIndicator.textColor = UIColor.darkGrayColor()
advertIndicator.backgroundColor = UIColor(red: 252/255.0, green: 246/255.0, blue: 220/255.0, alpha: 1.0)
advertIndicator.clipsToBounds = true
advertIndicator.layer.masksToBounds = true
advertIndicator.layer.cornerRadius = 3
}
func layoutAdAssets(adObject: MPNativeAd!) {
adObject.loadTitleIntoLabel(titleLabel)
adObject.loadTextIntoLabel(mainTextLabel)
adObject.loadIconIntoImageView(iconImageView)
}
}
It's quite magical, really, that I forgot to add the subviews to the contentView of the NativeAdCell. self.contentView.addSubview(mainTextLabel) etc solves #2.
Not sure how to get this to work with IBOutlets still. It would be a lot easier that way (removes need for programmatic constraints).

Resources