GIF animations in UIImageView in Swift - swift

How am I able to automatically load and run a GIF using UIImageView?
I have imported each image individually but now sure where to go from here.
Also it is to be executed in viewDidLoad, not by using an IBAction of any kind.

I don't know if UIImageView supports gif format but you can give bunch of image to UIImageView and it shows them in order. Here is how:
let imageView = UIImageView()
imageView.animationImages = imageArray // imageArray is array of UIImage instances
There is bunch of other properties and methods that control this behavior in UIImageView. You can find more information in documentation

Related

How to change pull to refresh icon of tableview in swift? [duplicate]

I have been looking around but couldn't find anything good about this.
I would like to customize the default UIRefeshControl with different loader, etc. So far I can only change tintColor & attributedTitle properties and most code I found are just basically creating a new "pulltorefresh" effect but what I want is to just use the UIRefreshControl and customize it a bit.
Is this possible?
You can't add different loader without accessing private APIs, but you can add background image:
UIImageView *rcImageView =
[[UIImageView alloc] initWithImage:
[UIImage imageNamed: #"refreshControl.png"]];
[self.refreshControl insertSubview:rcImageView atIndex:0];
assuming self is an instance of UITableViewController subclass.
Image size you need is 320x43px (#2x 640x86px), the middle area (approximately 35px) will be covered by the loader animation.
I show application logo there...

Issues setting up a back ground image and with UIImageView

On my iPhone app, I simply want to set a particular background image, which depends on whether it's an iPhone 5 or not.
So, I tried two approaches:
A) Using
self.view.backgroundColor = [UIColor colorWithPatternImage:backGroundimage];
B) Creating an UIImageView and setting up the image there. Code:
UIImageView *backgroundImageView = [[UIImageView alloc]initWithFrame:screenBounds];
[backgroundImageView setImage:[UIImage imageNamed:backGroundImage]];
[self.view addSubview:backgroundImageView];
But I am having issues with both of them:
Issues with Step A:
When I set the image through that way, I have to deal with the image scaling issues for different sizes of the screen. I use the following code to do the scalling:
UIGraphicsBeginImageContext(screenBounds.size);
[[UIImage imageNamed:backGroundImage] drawInRect:screenBounds];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
Another issue from Step A is that the image appears quite blurry. It doesn't have the same sharpness to it.
Issues with Step B:
With this, the image looks really crisp and sharp - just the way it should look.
But when I switch to another view using the following code, strangely enough the UIImageView backgroundImageView still appears on the second one. The code I use to switch views is:
[self presentViewController:secondViewController animated:YES completion:nil];
I even tried [backgroundImageView removeFromSuperview], but that doesn't solve anything either.
So what am I doing wrong? And how can I set up a picture as my background which is dependent on the size of the iphone?
Plan B is a good plan. Presenting another view controller should and will definitely hide your image view. If it isn't happening, then it's a problem with the creation of secondViewController, unrelated to the background image on the presenting VC.
Before presenting secondViewController, log it:
NSLog(#"presenting %#", secondViewController);
I'll bet a dollar that it's nil. If I'm right, let's have a look at how you initialize secondViewController. That's the problem, unrelated to the background image view.
Okay, I finally fixed this issue, although the cause of this issue is still puzzling to me.
To fix this, I had to create an IBOutlet property for UIImageView and hook it up on the XIB file.
The reason I was programmatically creating the UIImageView is because the size of the UIImageView depends on what size iPhone they are using. But for the IBOutlet (let's call it as UIImageViewOutlet, I simply used [self.UIImageViewOutlet setFrame:] to get the size and location that I wanted.
I also discovered that one of the buttons that I was programmatically creating, was still visible in the secondViewController. I ended up creating an Outlet on the XIB file for that one as well and used setFrame on it to position it properly.
If anyone who knows the reason of this problem, I will be very grateful.

UIImageView Image assignment and debug

When I've created an UIImageView, I can see its memory allocation in the debugger.
But I can't find its image property, which supposedly holds the actual image, in the debugger.
The 2 structures under the UIImageview in debugger are:
UIView
_storage
How can I otherwise tell, that a UIImageView actually contains an image?
Cheers
Kevin
Didn't know if you can check for UIImageView image in debugging
But you can check anywhere in your code by putting the line :
If(UIImageView.Image == nil )
Maybe this helps
You can use GDB command:
po [yourImageView image]
_storage - it is also a pointer to UIImage, i.e. your image property within UIImageView object

get image from previous controller

i am getting a picture from the iPhone's camera in one nib file (getPhoto.xib), where I save it into the camera library and also display it in an UIImageView (image1). Then, I have a next button which loads a different nib file (naming.nib) where the user can add a name for the image. How I can access the image from the first nib file so that I can display it in an UIImageView (image2) in the second nib file?
I'm trying the following, but nothing is displayed in image2:
UIImageView *image2= [[UIImageView alloc initWithImage:
[UIImageView imageNamed:image1]];
the imageNamed: method requires an NSString argument. Assuming that the name of image1 is actually "image1", you would do:
UIImageView *image2 = [[UIImageView alloc]
initWithImage:[UIImageView imageNamed:#"image1"]];
Note that the "name" here is the name you used when saving the image to the camera library.
call retain on image1 (the one you get from camera) and use it where ever u are currently trying to use image2

UIButton with image losing its newly set image

I've got a UIButton (custom, with image) set up in IB which is an IBOutlet in my code.
In my viewDidLoad method in the viewController, I am trying to change the existing image of the UIButton
UIImage *newbuttonimage = [UIImage imageNamed:#"newbuttonimage.png"];
testbutton.imageView.image = newbuttonimage;
OK, that works when starting the app, but whenever you interact with the button (press it), it changes to the original image (set in IB). There's no other code to change images in the whole project, so what is going on?
Incidentally, when I put the above code in any other part of my project it doesn't change the image.
You should try using setImage:forState instead of assigning the image directly; there are multiple states of a UIButton and, if not set properly, may yield unwanted behaviors (akin to the ones you're seeing).
Doh! I should have used this code instead:
UIImage *newbuttonimage = [UIImage imageNamed:#"newbuttonimage.png"];
[testbutton setImage:newbuttonimage forState:UIControlStateNormal];