Changing the image of an UIImage view with a button - objective-c

Hey, I have an ImageView in my app and I want the user to be able to change the image in that image view by clicking on a button.This is the code I got
In .h
#interface BGViewController : UIViewController {
IBOutlet UIImageView *image;
}
#property (nonatomic, retain) IBOutlet UIImageView *image;
-(IBAction)img1:(id)sender;
-(IBAction)img2:(id)sender;
and in .m
#synthesize image;
-(IBAction)img1:(id)sender; {
UIImage *image = [UIImage imageNamed: #"Main.png"];
}
-(IBAction)img2:(id)sender; {
UIImage *image = [UIImage imageNamed: #"Themes.png"];
}
There is one button for each image by the way!
The app builds but when I click on either one of the buttons nothings happens.

Replace
UIImage *image = [UIImage imageNamed: #"Main.png"];
and
UIImage *image = [UIImage imageNamed: #"Themes.png"];
with
image.image = [UIImage imageNamed:#"Main.png"];
and
image.image = [UIImage imageNamed:#"Themes.png"];
Now it should work fine :)

Simply set the image property of the UIImageView:
imageView.image = [UIImage imageNamed:#"Themes.png"];
You also have a syntax error in your method implementations, get rid of the semicolon (;) after your method signatures.
If I were designing this class, I'd use one action method and use the tag property of the sender argument to index into an array of NSString objects. (For the first button, the tag would be 0, while the second would be 1, etc.)
You should rename your UIImageView ivar to imageView to reduce ambiguity.
#interface BGViewController : UIViewController {
IBOutlet UIImageView *imageView;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
-(IBAction)changeImage:(id)sender;
#end
#implementation BGViewController
NSString *images[] = {
#"Main.png",
#"Themes.png"
};
#synthesize imageView;
-(IBAction)changeImage:(id)sender {
imageView.image = [UIImage imageNamed: images[sender.tag]];
}
#end

Related

My animation won't start

I've done this code to start animation when i click a button, when I click the button is should change what the button says and start the animation, but it only changes what the button says ? Is there anything wrong with my code?
#synthesize SwishImage;
#synthesize SwishButton;
- (IBAction) Swish {
if (SwishImage.isAnimating) {
[SwishImage stopAnimating];
[SwishButton setTitle:#"Swish" forState:UIControlStateNormal];
} else {
[SwishImage startAnimating];
[SwishButton setTitle:#"Searching" forState:UIControlStateNormal];
}
}
- (void)viewdidload
{
NSArray * SwishArray;
SwishArray = [[NSArray alloc] initWithObjects:
[UIImage imageNamed:#"Orange_dot.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
nil];
SwishImage.animationImages=SwishArray;
SwishImage.animationDuration = 1;
SwishImage.animationRepeatCount = 0;
[super viewDidLoad];
}
When I copied this into an empty Xcode project and linked everything correctly in Interface Builder the image view started animating. I assume something isn't correctly linked in Interface Builder.
That being said, your code is a bit awkward from a standards perspective. Variable names should begin with a lower case letter (and that includes properties). You don't need #synthesize anymore and there is a new shortcut for NSArray. Here is how your classes should look:
ViewController.h
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *swishImage;
#property (strong, nonatomic) IBOutlet UIButton *swishButton;
#end
ViewController.m
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *swishArray = #[
[UIImage imageNamed:#"Orange_dot.png"],
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed:#"3.png"],
[UIImage imageNamed:#"4.png"],
];
self.swishImage.animationImages=swishArray;
self.swishImage.animationDuration = 1;
self.swishImage.animationRepeatCount = 0;
}
- (IBAction)swish:(id)sender {
if (self.swishImage.isAnimating) {
[self.swishImage stopAnimating];
[self.swishButton setTitle:#"Swish" forState:UIControlStateNormal];
} else {
[self.swishImage startAnimating];
[self.swishButton setTitle:#"Searching" forState:UIControlStateNormal];
}
}
#end
Then just select your image view and go to the Connections Inspector. You should see a section titled Referencing Outlets, make sure that swishImage is connected to your view controller.

Why can't I add a UIScrollView to a UIImageView (with it working properly)

I'm working on an app. Here I describe a simplified version of a strange problem I am experiencing.
I have a storyboard created View Controller with a UIImageView also created in the storyboard.
I then have the following h and m files which aim to add a UIScrollView to the UIImageview and give the scrollview a container.
h file:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIImageView *imageView;
#property (nonatomic, strong) UIScrollView* scrollView;
#property (nonatomic, strong) UIView* container;
#end
m file:
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIImage *image = [UIImage imageNamed: #"new_phots_fake_port.png"];
//Add the image to the imageView I created in the Storyboard
[_imageView setImage:image];
_scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(60.0, 70.0, 522.0, 100.0)];
[_scrollView setBackgroundColor:[UIColor yellowColor]];
// Set up the container view to exist inside the scrollview:
CGSize containerSize = CGSizeMake(5000.0f, 730.0f);
_container = [[UIView alloc] initWithFrame:(CGRect){.origin=CGPointMake(0.0f, 0.0f), .size=containerSize}];
_container.backgroundColor = [UIColor redColor];
// Tell the scroll view the size of the contents
self.scrollView.contentSize = containerSize;
[_scrollView addSubview:_container];
[_imageView addSubview:_scrollView]; //This is what I want but doesn't allow scrolling
//[self.view addSubview:_scrollView]; //This allows scrolling but makes the imageView and Scrollview siblings. I need scrollview to be a child of imageview
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
So, the scrollview cannot seem to be added to the imageview (and function correctly). Why? What am I doing wrong?
Strangely, I also didn't use either of the delegate methods viewForZoomingInScrollView or scrollViewDidZoom. I'm not sure why these were not needed.
You need to set userInteractionEnabled on the UIImageView
_imageView.userInteractionEnabled = YES;

XCode 4.5 UiScrollView issue

I'm trying to implement an UIScrollView with Page Control with an array of images. I used same piece of code of another project, but it doesn't work with Xcode 4.5. ScrollView frame properties are all 0 while debugging, but the scrollview Outlet is well connected. Images doesn't show in the scroll. It is really strange. I have observed that properties are not synthesized automatically like previous versions. Here is the code:
#interface dashboardViewController : UIViewController <UIScrollViewDelegate>{
BOOL pageControlBeingUsed;
}
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) IBOutlet UIPageControl *pageControl;
- (IBAction)changePage;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageWithContentsOfFile:[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:#"fondo.jpg"]]];
pageControlBeingUsed=NO;
scrollView.delegate=self;
[self buildScrollView];
// Do any additional setup after loading the view.
}
-(void)buildScrollView{
NSArray *imagesQueue = [[NSArray alloc] initWithObjects:[UIImage imageNamed:#"icoblog.png"], [UIImage imageNamed:#"icogaleria.png"], [UIImage imageNamed:#"icobio.png"], nil];
for(int i = 0; i < imagesQueue.count; ++i) {
UIImageView *image = [[UIImageView alloc] initWithImage:[imagesQueue objectAtIndex:i]];
CGFloat xOrigin = i * (scrollView.frame.size.width);
image.frame = CGRectMake(xOrigin, 0, scrollView.frame.size.width, scrollView.frame.size.height);
[scrollView addSubview:image];
image=nil;
}
scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imagesQueue.count, self.scrollView.frame.size.height);
}
Many thanks for your help.
Select your scrollview in the storyboard and untick the "auto layout" under file properties
Try pushing "self." infront of all the "scrollView" you don't have it.

Xcode - How to change an image in UIImage View with only in one UIImageView and only one button

My code change the pic in two UIImageView with one button but I want only one UIImageView and only one button, how could I make it? Please help me. Thanks from now.
FirstView.h
#import <UIKit/UIKit.h>
#interface FirstView : UIViewController{
IBOutlet UIImageView *firstImage;
IBOutlet UIImageView *secondImage;
}
#property (nonatomic, retain) UIImageView *firstImage;
#property (nonatomic, retain) UIImageView *secondImage;
-(IBAction)buttonClicked;
#end
FirstView.m
#import "FirstView.h"
#implementation FirstView
#synthesize firstImage, secondImage;
- (IBAction)buttonClicked{
if (secondImage.image==nil) {
firstImage.image = nil; // this is useless, because the image is already in cache.
secondImage.image = [UIImage imageNamed:#"Mini.png"];
} else {
secondImage.image = nil; // this is useless, because the image is already in cache.
firstImage.image = [UIImage imageNamed:#"selo001.png"];
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// when view loaded, load the first image only.
firstImage.image = [UIImage imageNamed:#"selo001.png"];
}
I'm not quite shire what you are asking so this may not be what you want.
FirstView.h
#import <UIKit/UIKit.h>
#interface FirstView : UIViewController {
IBOutlet UIImageView *onlyImageVIew;
BOOL firstImage;
}
#property (nonatomic, retain) UIImageView *onlyImageVIew;
-(IBAction)buttonClicked;
#end
FirstView.m
#import "FirstView.h"
#implementation FirstView
#synthesize firstImage, secondImage;
- (IBAction)buttonClicked {
if (firstImage == YES) {
onlyImageView.image = [UIImage imageNamed:#"Mini.png"];
firstImage == NO
}
else {
onlyImageView.image = [UIImage imageNamed:#"selo001.png"];
firstImage == YES
}
}
- (void)viewDidLoad {
[super viewDidLoad];
// when view loaded, load the first image only.
firstImage = YES;
onlyImageView.image = [UIImage imageNamed:#"selo001.png"];
}

EXC_BAD_ACCESS questions

I am making a poetry app in which users can write your poems and save them to a file. When I click on the UITextView to add and edit text it instantly causes a EXC_BAD_ACCESS. Can somebody please help me as to why this is happening.
This is what I have in the .h file
#import <UIKit/UIKit.h>
#interface SecondViewController : UIViewController {
UITextView *newPoemTextView;
UIImageView *newPoemTextViewBackground;
}
#property (nonatomic, retain) IBOutlet UITextView *newPoemTextView;
#property (nonatomic, retain) IBOutlet UIImageView *newPoemTextViewBackground;
-(IBAction)closeKeyboard;
#end
and this is what I have in the .m file
#implementation SecondViewController
#synthesize newPoemTextView;
#synthesize newPoemTextViewBackground;
-(IBAction)closeKeyboard {
// This part will write the information to the file
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentTXTPath = [documentsDirectory stringByAppendingPathComponent:#"savedText.txt"];
NSString *savedString = newPoemTextView.text;
[savedString writeToFile:documentTXTPath atomically:YES];
//This part hopefully closes the keyboard correctly, cross fingers
[newPoemTextView resignFirstResponder];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
UIImage *image = [UIImage imageNamed: #"newPoemBackground.png"];
[newPoemTextViewBackground setImage:image];
UIColor *background = [[UIColor alloc] initWithPatternImage:[UIImage
imageNamed:#"background.png"]];
self.view.backgroundColor = background;
[background release];
[super viewDidLoad];
}
You should synthesize both the property in SecondViewController.m file.
#synthesize newPoemTextView;
#synthesize newPoemTextViewBackground;
You should also implement Keyboard's all method, that will help you a lot.