Image won't appear in Xcode - xcode

I'm a first time programmer and i'm trying to make a soundboard app. Its not finished yet and it is pretty basic. I have a main menu with one button. When this button is pressed, a different background image should appear along with a back button. The image does not change. here is the code:
-(IBAction)redgradient {
UIImage *img = [UIImage imageNamed:#"redgradient.jpg"];
[imageView setImage:img];
}
-(IBAction)redgradient2 {
UIImage *img = [UIImage imageNamed:#"redgradient2.png"];
[imageView setImage:img];
}
I have mentioned the IBActions in the H file and used an IBOutlet of ImageView. Help is appreciated.

Here's a more simple way to do that, test if this works better:
-(IBAction)redgradient {
imageView.image = [UIImage imageNamed:#"redgradient.jpg"];
}
-(IBAction)redgradient2 {
imageView.image = [UIImage imageNamed:#"redgradient2.png"];
}
Check if the images ends on .jpg or .png if that is the problem.

Here are a few things to check:
The image files have been added to the project.
The image names are exactly as they appear in the project (case sensitive and correct format).
The connections are properly established in the IB.

Related

UIImage Animation Reverting Back To Original Position

Im pretty new to coding but I'm starting to get the hang of the basics.
how to make an image stay in its new position after an animation?
Example:
I'm giving an animating object a random position, however, the animation causes the object not to animate at the random position, but instead animate at the position it was given in the view controller. This also happens when I animate a completly different object.
Code I used:
int Random1x;
int Random1y;
IBOutlet UIButton *Start;
IBOutlet UIImageview *Object2;
-(void)ObjectMoving;
-(void)Object2Animate;
-(IBAction)Start:(id)sender{
[self ObjectMoving];
[self Object2Animate];
}
-(void)Object2Animate {
Object2.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"2.png"],
[UIImage imageNamed: #"3.png"],
[UIImage imageNamed: #"4.png"],
[UIImage imageNamed: #"1.png"], nil];
Object2.animationDuration = .5
[Object2 setanimationRepeatCount: 0]
[Object2 startAnimating];
}
-(void)ObjectMoving {
Random1y = arc4random() % 466;
Random1y = Random1y + 60;
Random1x = arc4random() % 288;
Object2.center = CGPointMake(Random1x, Random1y);
}
I'd greatly appreciate help, thank you!
If you go to your story board file and click on the View Controller and then file inspector you will see a box for Auto Layout
Post back if that worked.
If you do need to use auto layout then you would have to figure out a different way of moving the image.

UIImageView photo transition effect Xcode 5

I want to add a transition effect between images like fade or anything else.
Here's my code:
-(IBAction)startSlideShow:(id)sender;
{
self.imageView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"slide01.png"],
[UIImage imageNamed:#"slide02.png"],
[UIImage imageNamed:#"slide03.png"],
[UIImage imageNamed:#"slide04.png"],
[UIImage imageNamed:#"slide05.png"],
nil];
self.imageView.animationDuration = 15.0;
self.imageView.animationRepeatCount = 0;
[self.imageView startAnimating];
}
Try this, I think you looking for this one,.. github.com/jberlana/JBKenBurns
(or)
I found this on someother search, try this too..
http://www.raywenderlich.com/10518/how-to-use-uiscrollview-to-scroll-and-zoom-content
Other simple way to achieve: Steps to achieve
Add all the images in one scroll view.
Enable paging for that scroll view.
Based on the content offset of the scroll view highlight the appropriate dot(indicator in the bottom).

Ignore the image cache on setImage:

I have the following code
NSString *myPath = [NSString stringWithFormat:#"../Documents/%#",#"bla.png"];
[imgView setImage:[UIImage imageNamed:myPath]]; // works fine
[self updateMyImage:[UIImage imageNamed:myPath]]; // loads another image as bla.png (works fine)
[imgView setImage:[UIImage imageNamed:myPath]]; // displays the old image, *not* the new one
The image is loaded in updateMyImage: (I open it (from the simulator's disk) and it is the "new" one)
but imgView displays the "old" one. I guess its a cache issue. Since the iOS has loaded the [UIImage imageNamed:#"bla.png"]
once, it thinks that this is the same one. How can I "flush" the cache and tell it to reload the image?
setImage: loads (as you corectly guess) from the cache.
Look for it in the 'UIImage Class Reference' (on xcode's Organizer window)
Few lines down it gives you the answer: use the initWithContentsOfFile: method.
So, after you change the image do this:
NSString *myAbsolutePath = [NSString stringWithFormat:#"%#/%#", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0],#"bla.png"]];
UIImage *myImg = [[UIImage alloc] initWithContentsOfFile:myAbsolutePath];
[imgView setImage:myImg];
and now imgView will have the new image (as long as a img file exists at myAbsolutePath).

How to change image within a customised UIImageView class

I'm new to Xcode. This is probably a very simple question to you, but I wonder if anyone can help me.
What I want to do is to load an image in a customised UIImageView, when clicking the image, the imageview will change to another image. When I run the simulator, there are no error messages, but the second image does not load after clicking.
I've declared a UIImageView subclass dragView. In the dragView.m, I have such codes:
-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self changePicture];
}
-(void) changePicture {
NSString *path = [[NSHomeDirectory() stringByAppendingPathComponent:#"Documents"] stringByAppendingPathComponent:#"Red Flower.png"];
self.image = [UIImage imageWithContentsOfFile:path];
}
By the way, I have enabled the interaction. Many thanks!
You probably need to tell the OS to update the view by calling [self setNeedsDisplay:YES].

Xcode : Wallpapers with UIButton

I want to make an wallpaper iphone app.
Now if i have for example 100 wallpapers , do I have to create 100 nib files and 100 .m and .h files and 100 UIButtons for to save the wallpaper and 100 UIButton for to go to the previous or next wallpaper ?
Is there any other way to do this ?
Thanks !
yes sure.
save your hundred wallpapers in the resources folder. The are named like this "wp_1.png", "wp_2.png" etc.
Then you create a nib with the buttons (previous, next and save)
In the uiviewcontroller you'll have an int property. For instance you call it n or something like that. I would suggest adding another property for your wallpaper (UIImageView) Then you implement the methods in the view controller.
-(void)awakeFromNib {
self.n = 1;
UIImage* wallpaper = [UIImage imageNamed:#"wp_1.png"];
UIImageView* view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.wallpaper = view;
view.image=wallpaper;
[self.view addSubview:view];
}
-(IBAction)next:(id)sender {
self.n++; // increases n by 1;
[self.wallpaper removeFromSuperview];
NSString* name = [NSString stringWithFormat:#"wp_%i.png", self.n];
UIImage* wallpaper = [UIImage imageNamed:name];
UIImageView* view = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,480)];
self.wallpaper = view;
view.image=wallpaper;
[self.view addSubview:view];
}
something like that. hope that helps
Of course not. I would use a UITableView for that. Or even better a custom view that looks similar to the Photos app on the iphone.
You don't want to use a UIButton to go to the previous or next photo. You want to use a UIScrollView in paging mode.
But you don't want the user to browse through your images in that way in the first place.
Create a index screen that has a UIScrollView (non-paging this time) with thumbnails of all your images. And when one is tapped it zooms in with a nice animation and you can flick to the next or previous image with the paging UIScrollView I mentioned earlier.
You know, like the Photos app.
in the header (.h file) add something like that:
int n;
UIImageView* wallpaper;
}
#property (readwrite) int n;
#property (readwrite, retain) UIImageView* wallpaper;
in the implemention fil (.m file) :
#synthesize n, wallpaper;

Resources