Another way to declare an imageview in Objective-C - ios

I found an objective C project that I try to modify, As you can see, I get a function that create an UIImageView:
- (UIImageView*)homePage:(YouzikHomePageView*)homePageView imageDataForImageView:(UIImageView*)imageView;
{
[imageView setImage:[UIImage imageNamed:#"background"]];
return imageView;
}
Problem is that I want to modify the image displayed each 10 seconds, this require to get the UIImageView declared in ViewDidLoad method, is it possible ? I tried this:
- (void)viewDidLoad
{
[super viewDidLoad];
[self setupNa...
[self.homePa...
[self hid...
[self setuphomePag...
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background"]];
[self.homePageView addSubview:imageView];
}
But it's not dispaying anything, thanks for all, please excuse my crappy english.

What your code is doing is creating and adding imageViews to a subview.
That's probably not what you intended to do, and you don't want to keep creating and adding more imageViews, every 10 seconds, as you'll eventually run out of memory.
You generally would have an imageView property on your view controller, and change its image, by setting its image property.
In HomeViewController.h
#property (weak, nonatomic) IBOutlet UIImageView *myImageView;
In HomeViewController.m
self.myImageView.image = [UIImage imageNamed:#"background"];
As for changing the image every ten seconds, you would setup a timer
myTimer = [NSTimer scheduledTimerWithTimeInterval:10.0 target:self selector:#selector(changeImage) userInfo:nil repeats:YES];
and set a new image in your changeImage method
- (void)changeImage
{
self.myImageView.image = ...;
}

Related

Where to programmatically declare images in Xcode

I'm trying to build a timer app. When the timer hits a certain number (say 5), I want an image to appear. I have the timer working, and the image shows up, but when I declare an if statement to hide the image when the timer has not reached 5, the image shows up anyways. Furthermore, when I declare the UIImageview at the bottom, Xcode reads me the error of "Local declaration of 'images' hides instance variable.
Thank you for any help you can offer! I just started programming.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController;
-(IBAction)Start:(id)sender; {
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target: self selector:#selector(TimerCount) userInfo: nil repeats:YES];
}
-(void)TimerCount{
CountNumber = CountNumber + 1;
TimerDisplay.text=[NSString stringWithFormat:#"%i", CountNumber];
if(CountNumber < 6) {
imagesx.hidden = YES;
}
if(CountNumber > 6){
imagesx.hidden = NO;
}
}
-(IBAction)UIImage:(id) sender{
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"Images.jpg"]];
if (CountNumber < 6){
imagesx.hidden = YES;
}
}
-(IBAction)Stop:(id)sender{
[Timer invalidate];
TimerDisplay.text=[NSString stringWithFormat:#"%i", CountNumber];
}
-(IBAction)Reset:(id)sender{
CountNumber = 0;
[Timer invalidate];
CountNumber = 0;
TimerDisplay.text=[NSString stringWithFormat:#"%i", CountNumber];
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimerCount) userInfo:nil repeats:YES];
[Timer invalidate];
}
-(IBAction)Restart:(id)sender{
CountNumber = 0;
TimerDisplay.text = [NSString stringWithFormat:#"%i", CountNumber];
CountNumber = 0;
Timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:#selector(TimerCount) userInfo:nil repeats: YES];
if (CountNumber < 6){
imagesx.hidden = YES;
}
if (CountNumber > 6){
imagesx.hidden = NO;
}
}
- (void)viewDidLoad{
//create UIImage view programmatically
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIImageView *imagesx =[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
imagesx.image=[UIImage imageNamed:#"images.jpeg"];
[self.view addSubview:imagesx];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
View Controller.h:
#import <UIKit/UIKit.h>
int CountNumber;
#interface ViewController : UIViewController
{
IBOutlet UILabel *TimerDisplay;
UIImageView *imagesx;
NSTimer *Timer;
}
-(void)TimerCount;
-(IBAction)Start:(id)sender;
-(IBAction)Stop:(id)sender;
-(IBAction)Restart:(id)sender;
#property (strong, nonatomic) IBOutlet UILabel *FirstiPhoneapp;
#property (strong, nonatomic) IBOutlet UILabel *ByJuliaTu;
#property (strong, nonatomic) IBOutlet UIButton *Start;
#property (strong, nonatomic) IBOutlet UIButton *Reset;
#property (strong, nonatomic) IBOutlet UIButton *Terminate;
#property (strong, nonatomic) IBOutlet UIButton *Restart;
#end
You declared your image view as an instance variable with the name "imagesx"
#interface ViewController : UIViewController
{
IBOutlet UILabel *TimerDisplay;
UIImageView *imagesx; <-----your ivar
NSTimer *Timer;
}
So now your view controller has a spot in memory to place an image view.
when viewDidLoad: is called you are doing this:
UIImageView *imagesx =[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
This variable (pointer to a UIImageView) has the same name is the instance variable. This will hide access to that ivar and create a new spot in memory for this local version. In other words when you do this in viewDidLoad:
- (void)viewDidLoad{
//create UIImage view programmatically
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
// You are declaring a new image view here with the same name so it "overrides" or hides the ivar by creating a new local one:
UIImageView *imagesx =[[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200, 200)];
// You are assigning your newly created image view to this local variable
imagesx.image=[UIImage imageNamed:#"Matthijs"];
// adding it to the view, which works but:
[self.view addSubview:imagesx];
// the method returns, this local variable is gone and the ivar is still nil
}
// The ivar is never getting a pointer to your newly created image view. You can't access it after the method returns.
if you put a breakpoint in the timerCount method
Your image view was never set to the ivar (it is nil) so you can't get to it in order to set it to hidden.
The fix is simple. Don't redeclare a new image view in viewDidLoad:, just assign it to your ivar:
imagesx = [[UIImageView alloc] initWithFrame:CGRectMake(50, 50, 200,200)];
Or if you do, declare a new one and change the name:
UIImageView* imageView = [[UIImageView alloc]initWithFrame:CGRectMake(50.0f, 50.0f, 200.0f, 200.0f)];
then assign that to your ivar
imagesx = imageView;
Your code is very hard to follow. You shouldn't create ivars anymore, you can just create a proper for the NSTimer and the UIImageView. Also I'm not sure why you created outlets to the buttons if you're just using the IBActions.
In your viewDidLoad you are redeclaring imagesx and adding it as a subview so when you use the ivar in the timer call, you're actually looking at a nil pointer.
You should declare your NSTimer and the UImageView as properties:
#property (strong, nonatomic) NSTimer * timer;
#property (weak, nonatomic) IBOutlet UIImageView * imageView; // Connect this to an ImageView in the storyboard instead of creating it in code
In your .m file you should access the timer and the image as properties:
self.timer =
self.imageView.image = [UIImage imageNamed:#"..."];

UIImageView Animation Not Working

Just started Objective-C - trying to make 4 images fadein/fadeout continuously.
The code below is not working. I know the button is working because the text of my label changes, however no pictures animate. No errors either ...
h.
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
{
IBOutlet UIImageView *viewer;
}
#property (nonatomic, retain) UIImageView *viewer;
#property (nonatomic,retain) IBOutlet UILabel *label1;
#end
m.
-(IBAction)startImageViewAnimation
{
label1.text = #"CHECK?";
NSArray *animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"backgrounds_app1-01.png"],
[UIImage imageNamed:#"backgrounds_app2-02.png"],
[UIImage imageNamed:#"backgrounds_app3-03.png"],
[UIImage imageNamed:#"backgrounds_app4-04.png"],
nil];
viewer = [[UIImageView alloc] initWithFrame:self.view.frame];
viewer.animationImages = animationImages ;
viewer.animationRepeatCount = 2;
viewer.animationDuration= 4.0;
[viewer startAnimating];
[NSTimer scheduledTimerWithTimeInterval:4.0 target:self
selector:#selector(animationDone:)
userInfo:nil repeats:NO];
}
-(void)animationDone:(NSTimer*)inTimer
{
[inTimer invalidate];
inTimer = nil;
NSLog(#"animationDone ");
}
After initialising the image view, you'll have to add it as a subview:
viewer = [[UIImageView alloc] initWithFrame:self.view.bounds];
[self.view addSubview:viewer];

UIView setNeedsDisplay doesn't work

Simple code is here:
Custom MyButton.h
#interface PaiLifeReadingListButton : UIButton
#property (strong, nonatomic) UIImageView *logoImageView;
#end
Custom MyButton.m
#implementation PaiLifeReadingListButton
#synthesize logoImageView = _logoImageView;
-(id)init
{
_logoImageView = [[UIImageView alloc] initWithFrame:CGRectMake(33.0, 20.0, 80.0, 80.0)];
[_logoImageView setImage:[UIImage imageNamed:#"pic"]];
[self addSubview: _logoImageView];
}
#end
Custom MyView.h:
#property (strong, nonatomic) Mybutton *mybutton;
Custom MyView.m:
-(id) init
{
myButton = [[MyButton alloc] init];
[self addSubview:myButton];
}
ViewController.m:
#synthesize myView;
- (void)viewDidLoad
{
myView = [[Myview alloc] init];
[myView.myButton addTarget:self action:#selector(pressed:) forControlEvents:UIControlEventTouchDown];
self.view = myView;
}
-(void)pressed : (MyButton *)button
{
UIImageView *newImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"other-pic"] highlightedImage:nil];
myView.myButton.logImageView = newImageView;
[self.view setNeedsDisplay]; //----this does not work
}
When I press the button, the imageview does not change. How can I fix this?
Thank you very much!
First thing , you don't need setNeedsDisplay here. setNeedsDisplay is used for redrawing the view (it internally calls the drawRect: method of the view if required) which is not what you want.
Second, you have given the class of your button as MyButton,but in the actual .h and .m files, it is given as #interface PaiLifeReadingListButton and #implementation PaiLifeReadingListButton respectively.
Another thing, the view and button is not initialized with a frame. But I assume you have done that already, since your only problem is the image view not changing.
In your code in pressed method you should change views in the hierarchy, but you change only the object member. You can do something like
myView.myButton.logImageView.image = [UIImage imageNamed:#"other-pic"];

Animating UIImageView frame by frame

I have an idea about how to add animated UIImageView using frame by frame method BUT my question is about How to animate UIImageView ALREADY added on view controller storyboard as IBOutlet UIImageView .
what will be changed at this peace of code ?!
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"bookmark1.png"],
[UIImage imageNamed:#"bookmark2.png"],
[UIImage imageNamed:#"bookmark3.png"],
[UIImage imageNamed:#"bookmark4.png"],
[UIImage imageNamed:#"bookmark5.png"],nil];
myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:CGRectMake(0, 0, 131, 125)];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25;
myAnimatedView.animationRepeatCount = 1;
[myAnimatedView startAnimating];
[self.navigationController.view addSubview:myAnimatedView];
I want to animate this image like that over my viewController
http://imageshack.us/a/img717/3051/bookmarkl.gif
It's even easier. First, add in .h file:
#property (nonatomic, retain) IBOutlet UIImageView *iV;
After, connect this outlet to the actual UIImageView on your storyboard. Change your code:
iV.animationImages = myImages;
iV.animationDuration = 0.25;
iV.animationRepeatCount = 1;
[iV startAnimating];
And that's all. Hope this helped
p.s. And yes, don't forget iV = nil; in - (void)viewDidUnload method
UPD: added
[self performSelector:#selector(animationDone) withObject:nil afterDelay:0.25];
After startAnimating call, and obviously added animationDone method:
- (void)animationDone {
[iV stopAnimating];
[iV setImage:[UIImage imageNamed:#"bookmark5.png"]];
}
Well it will be pretty much the same. You dont need to allocate your image view [[UIImageView alloc] init] nor do you need to add it to the viewController. The rest is the same.

[UIImageView _isResizable]: unrecognized selector sent to instance SIGABRT

I've got this code trying to run a simple set of images in a cycle. All I have in the app is one UIImageView declared in my View Controller's .h file:
#property (strong, nonatomic) IBOutlet UIImageView *imageDisplay;
And the following in my .m file's viewDidLoad method:
NSMutableArray *imageView = [[NSMutableArray alloc] init];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim1.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim2.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim3.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim4.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim5.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim6.png"]]];
[imageView addObject:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"EyeAnim7.png"]]];
imageDisplay.animationImages = imageView;
imageDisplay.animationDuration = 0.25;
imageDisplay.animationRepeatCount = 50;
[imageDisplay startAnimating];
The code seems to be crashing on the "imageDisplay.animationImages" line, as if I create the UIImageView, create its getter and setter, and build, it's fine until I uncomment that line. If I do uncomment it, it keeps giving me the error until I delete the UIImageView and create a new one.
Not too sure what's happening, any help appreciated!
I am very new to objective-c and I have been getting this error also, but for a different reason. I just wanted to post my solution for anyone else who may be struggling.
So basically I have a custom class called ImagesDetailViewController which inherits from UIViewController and has an image property.
#interface ImagesDetailViewController : UIViewController
#property (strong, nonatomic) UIImage *image;
#end
I then connected my class to my UIImageView on my storyboard like so
#interface ImagesDetailViewController ()
#property (nonatomic, weak) IBOutlet UIImageView *imageView;
#end
In my viewDidLoad method I was trying to set the image for my image view like this and getting the error mentioned above (my image variable gets initialised in a prepareForSegue method)
- (void)viewDidLoad
{
[super viewDidLoad];
[self.imageView setImage:self.image];
}
So I was stumped as I bet you are all too. The problem had to do with the storyboard. Clicking on my UIImageView and then navigating to the Connections Inspector, I had somehow created 2 referencing outlets (oops...) and one was pointing to a variable called image. So when the program was running [self.imageView setImage:self.image] self.image was actually an instance of UIImageView instead of UIImage.
animationImages array MUST contain only UIImage objects. Your array contains UIImageView objects.
Also your code is unsafe - if one of the resources will not exist app will crash (trying to add nil object to the mutable array). This will be much safer:
#define kNumberOfImages 7
NSMutableArray *imageView = [[NSMutableArray alloc] init];
for(NSUInteger i = 1; i <= kNumberOfImages; i++) {
UIImage *anImage = [UIImage imageNamed:[NSString stringWithFormat:#"EyeAnim%d", i]];
if(anImage) {
[imageView addObject:anImage];
}
}
self.imageDisplay.animationImages = imageView;
self.imageDisplay.animationDuration = 0.25;
self.imageDisplay.animationRepeatCount = 50;
[self.imageDisplay startAnimating];

Resources