In my app I am setting the image of a UIImageView as follows:
override func viewWillAppear(animated: Bool) {
// ...
self.currentPlayer.image = UIImage(named: appDelegate.appSupportDirectory.stringByAppendingPathComponent(currentPlayer!.id!.UUIDString + ".jpg"))
}
In a different view, I allow the user to change the image, namely replace the file with a new one while keeping the name. When I return to this view the old picture is still shown. The app seems to remember the outdated picture even when I load a different player profile in between. How can I force the UIImageView to refresh?
Solution found. Change the code to:
override func viewWillAppear(animated: Bool) {
// ...
self.currentPlayer.image = UIImage(contentsOfFile: appDelegate.appSupportDirectory.stringByAppendingPathComponent(currentPlayer!.id!.UUIDString + ".jpg"))
}
Related
I am new to iOS development, I have created android app where app gets sets of images (sometimes over 100 images) from urls and loads into imageView with zoomEnabled Inside ViewPager.
Now I want to create same app for iOS, I have found this lib ImageSliderShow. Problem with this is it shows fullscreen ONLY when user did tap on selected image. I have been struggling to presentFullScreenController on viewDidLoad with no lock.
I only want image to be shown in fullScreen, example:
Select Category A From CategoryVC -> Loads ImageSlideVC, gets set of images from server, show in FullScreenView.
How can i achieve this? Adding this:
slideshow.presentFullScreenController(from: self)
on viewDidLoad didn't work:
#IBOutlet var slideshow: ImageSlideshow!
let kingfisherSource = [KingfisherSource(urlString: "https://images.unsplash.com/photo-1447746249824-4be4e1b76d66?w=1080")!,
KingfisherSource(urlString: "https://images.unsplash.com/photo-1447746249824-4be4e1b76d66?w=1080")!,
KingfisherSource(urlString: "https://images.unsplash.com/photo-1463595373836-6e0b0a8ee322?w=1080")!]
override func viewDidLoad() {
super.viewDidLoad()
slideshow.setImageInputs(kingfisherSource)
slideshow.presentFullScreenController(from: self)
}
I dont get any error, output is same as before (shows slideshow in smallview with no zoom)
Please help
EDIT
by doing in viewDidAppear, after split second view is loaded its loads into fullscreen. First its smallview then shows in fullscreen. I think i have to do something inside setImageInputs
this is what its looks like :
open func setImageInputs(_ inputs: [InputSource]) {
self.images = inputs
self.pageControl.numberOfPages = inputs.count
// in circular mode we add dummy first and last image to enable smooth scrolling
if circular && images.count > 1 {
var scImages = [InputSource]()
if let last = images.last {
scImages.append(last)
}
scImages += images
if let first = images.first {
scImages.append(first)
}
self.scrollViewImages = scImages
} else {
self.scrollViewImages = images
}
reloadScrollView()
layoutScrollView()
layoutPageControl()
setTimerIfNeeded()
}
try to do it in viewDidAppear
#IBOutlet var slideshow: ImageSlideshow!
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
slideshow.setImageInputs(kingfisherSource)
slideshow.presentFullScreenController(from: self)
}
Presenting viewController in ViewDidLoad is a bad practice.
How Do I add multipleImages for the top part and have it scrollable without moving the rest of the content? Also, how do I have the little dots at the bottom to indicate which image I'm on? My code for the image is down below.
import SDWebImage
#IBOutlet weak var headerImage: UIImageView!
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
let imageUrl:NSURL? = NSURL(string: headerPhoto!)
if let url = imageUrl {
headerImage.sd_setImage(with: url as URL!)
}
}
You are looking for something called image slide show or image slider.
Instead of creating them by yourself which requires lots of effort, here is a GitHub library that is easy to use.
Basicly the way slider works is that it is a horizontal scroll view and each cell in the scroll view is an image so that you can scroll. Then, put a fixed page index element on the bottom of the scroll view to tell you which image you are currently at.
To use it, create a UIView in your viewcontroller and set both class and module to ImageSlideshow. Then connect it to your ViewController.swift as IBOutLet.
Then create an array of image urls
let alamofireSource = [AlamofireSource(urlString: "imgurl1")!, AlamofireSource(urlString: "imgurl2")!, AlamofireSource(urlString: "imgurl3")!]
And finally in your viewDidLoad() function:
override func viewDidLoad() {
super.viewDidLoad()
slideshow.backgroundColor = UIColor.white
slideshow.slideshowInterval = 5.0
slideshow.pageControlPosition = PageControlPosition.underScrollView
slideshow.pageControl.currentPageIndicatorTintColor = UIColor.lightGray
slideshow.pageControl.pageIndicatorTintColor = UIColor.black
slideshow.contentScaleMode = UIViewContentMode.scaleAspectFill
slideshow.setImageInputs(alamofireSource)
}
Swift App Xib - Second ViewController - Labels not showing for 30 seconds but Buttons and other controls showing immediately. What could this be / how can I solve this?
Xcode 7.3.1
Swift 2
This is happening on the device and in the simulator
---> Video of Issue <-----
I present from first viewController like this:
secondViewController.user = user
self.presentViewController(secondViewController, animated: true, completion: nil)
Second View Controller
override func viewDidLoad() {
super.viewDidLoad()
print("ViewController did load")
print("selected facility is: ", user?.selectedFacility)
// Do any additional setup after loading the view.
}
// required init(coder aDecoder: NSCoder) {
// super.init(nibName: "ListViewController", bundle: NSBundle.mainBundle())
// }
override func awakeFromNib() {
print("awake from nib")
}
override func viewWillAppear(animated: Bool) {
print("viewWillAppear")
}
override func viewDidAppear(animated: Bool) {
print("viewDidAppear")
}
override func viewDidDisappear(animated: Bool) {
print("viewDidDisappear")
}
UPDATE
If I set the text in viewDidLoad the label appears at the same time as everything else.
override func viewDidLoad() {
super.viewDidLoad()
print("ListViewController did load")
labelX.text = "heyVC"
Adding the outlet itself didn't change anything. It was actually programmatically setting the text that fixed it. I think this is a bug in IB. I will file with apple.
Bug number is 27029176
See if you have set some custom font of label and the font file could not be found by system(like if you have deleted the file). If system font is not set in Label , try setting system font and re-run to confirm.
Are you doing anything with NSURL / NSMutableURLRequest / dataTaskWithRequest?
I am experiencing exactly the same thing with the 30sec delay. When I am commenting out my code which is fetching data from an URL (with the above mentioned methods) the labels are showing instantly. When the code is executed, the labels are displayed with a delay. I guess the fetching somehow makes things asynchronous.
Not exactly a solution but maybe a starting point for further research. Please let me/us know if you have new ideas or the solution. Thanks. :-)
I wanna display a couple of images from the assets folders, into my application. so I wanna make a page view.
First, images will be inside collection view, then on click, the image will be full screen. Then the user can slide between the images by swiping right and left as shown in the following photo:
I found this tutorial:
PhotosGalleryApp
Updated:
I have this in my storyboard:
Now in GaleryViewController I show the images in cells
when user click on it, I open the image in fullscreen in PhotoViewController.
PhotoViewController.swift :
import UIKit
class PhotoViewController: UIViewController{
#IBOutlet weak var imageView: UIImageView!
var index: Int = 0;
var pageViewController : UIPageViewController?
#IBAction func btnCancelClicked(sender: AnyObject) {
self.navigationController?.popToRootViewControllerAnimated(true);
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
initUI();
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
override func viewWillAppear(animated: Bool) {
self.navigationController?.hidesBarsOnTap = true;
self.navigationController?.hidesBarsOnSwipe = true;
displayPhoto()
}
func initUI() -> Void {
// pageViewController = UIPageViewController(transitionStyle: .Scroll, navigationOrientation: .Horizontal, options: nil)
// pageViewController!.dataSource = self
}
func displayPhoto() {
self.imageView.image = UIImage(named: Constants.Statics.images[index])
}
I have the images in static structure so i can access them anywhere:
class Constants {
struct Statics {
static let images = ["1.jpg","2.jpg","3.jpg","4.jpg","5.jpg","7.jpg","8.jpg"]
}
}
My problem is: I want to add the feature that allow me to swipe between pictures inside the PhotoViewController.
Any ideas?
Thank you
You can do one of the following two things to achieve your goal :
Make a modal segue of the cell to the next UICollectionViewController where the images are showed in full screen, and in the prepareForSegue pass all the data (images) you need to show and where exactly you are in this moment (indexOfImage).
You can watch the didSelectItemAtIndexPath and then pass all the data to one instance of the next UICollectionViewController you want to show with the presentViewController func.
But it's very important that in the next UICollectionViewController you have set pageEnabled = true in code or in the Interface Builder (I though is much easy to do.)
UPDATE:
Very good tutorials on how to make a slide show of images using an UIScrollView or an UICollectionView :
How To Use UIScrollView to Scroll and Zoom Content
Creating a Paged Photo Gallery With a UICollectionView
I hope this help you.
I am new to Swift programming and really have high hopes from all the experts out there. While creating a simple app I ran into memory issues and my app crashed. Below are more details about my app..
My app has an image view, three images and three buttons in toolbar. All my app does right now is changes image with button press. Example if you press say button1, it will load image1, if you press button2 it would load image 2 and if you press button 3 it would load image 3.
This app crash after 1 cycle, like I can tap all the three buttons and it would work, however if I tap on any button after that it would just crash.
I am testing this on iPad mini. Works perfectly fine on simulator. Any help would be greatly appreciated. I head somewhere that UIImage (named:) can be an issue, but I don't know how to replace that with anything else in Swift.
Here is the code for it
import UIKit
class ViewController: UIViewController
{
#IBOutlet weak var TestImage: UIImageView!
#IBAction func Button1(sender: AnyObject)
{
TestImage.image = UIImage (named: "Image1.jpg")
}
#IBAction func Button2(sender: AnyObject)
{
TestImage.image = UIImage (named: "Image2.jpg")
}
#IBAction func Button3(sender: AnyObject)
{
TestImage.image = UIImage (named: "Image1.jpg")
}
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning()
{
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
The problem with UIImage(named: name) is that it caches the images. As the documentation says:
If you have an image file that will only be displayed once and wish to ensure that it does not get added to the system’s cache, you should instead create your image using imageWithContentsOfFile:. This will keep your single-use image out of the system image cache, potentially improving the memory use characteristics of your app.
Thus, if you have memory problems arising from the size/number of images in the system cache, you can use UIImage(contentsOfFile: path) rather than UIImage(named: name):
if let path = NSBundle.mainBundle().pathForResource("Image1", ofType: "jpg") {
TestImage.image = UIImage(contentsOfFile:path)
}
I reduced the size using png 8 format in ps and it fixed the issue for me.. Thanks all for your help :-)