Why cant I set UIImage in UIimageview? - ios

I'm trying to set an image from another ViewController using properties. But It doesn't work. Someone said that you only can change image from inside the Class. But i tried and its not working either way. Any ideas?
//viewcontroller1
ViewController *viewcont = [[ViewController alloc]initWithNibName:#"ViewController" bundle:nil];
[self presentViewController:viewcont animated:NO completion:nil];
[viewcont somemethodThatIscallemFromOutside];
//viewcontroller2
-(void)somemethodThatIscallemFromOutside{
if (!r12 && g12 && !b12) {
green.image = [UIImage imageNamed:#"Green"];
self.Greenscore.textColor = [UIColor greenColor];
self.readyplayer2.image = [UIImage imageNamed:#"Greenready"];
}
}

First of all no such method available
[UIImage imagename:#"Image1.png"]
Replace with
[UIImage imageNamed:#"Image1.png"];
you should see also myimageview is intiliazed or not?

Try this:
myimageview.image = [UIImage imageNamed:#"Image1.png"];
Hope this helps.. :)

Replace this with your code,your mistake in "_" which is be"="
[myimageview setImage:[UIImage imageNamed:#"Image1.png"]];

Related

ios change background image dependant on device

I'm trying to set up my app so that on an iPhone4s, a different background image is displayed, but the code below produces a black background. Can anyone see what I'm doing wrong?
Thank you
UIImageView *bgImageView = [[UIImageView alloc] initWithFrame: self.view.bounds];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
bgImageView.image = [UIImage imageNamed: #"background_rest-ipad.png"];
}
else if ( [GUIHelper isPhone5] ) {
bgImageView.image = [UIImage imageNamed: #"info-screen-bg#2x.png"];
}
else {
bgImageView.image = [UIImage imageNamed: #"info-screen-bg-i4s.png"];
}
bgImageView.userInteractionEnabled = YES;
bgImageView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview: bgImageView];
You can use Asset Catalog and it will manage your images based on device...
Thought, with your code, make sure the names of the image files are correct and the files itself are present.

Problems showing badge on UITabBarItem with UIImageRenderingModeAlwaysOriginal image

I'm trying to show a badge on some UITabBarItems that are configured with an image with UIImageRenderingModeAlwaysOriginal. I'm doing this because the desired effect is something like a raised UITabBarItem a-la Instagram. This is the code I'm using to test this (it's in a UITabBarController category):
- (void) replaceImageForTab:(int)itemIndex
defaultImage:(UIImage *)defaultImage
selectedImage:(UIImage *)selectedImage
title:(NSString *)title
{
NSArray *viewControllers = [self viewControllers];
UIViewController *vc = [viewControllers objectAtIndex:itemIndex];
[vc.tabBarItem setImage:[defaultImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[vc.tabBarItem setSelectedImage:[selectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[vc.tabBarItem setTitle:title];
[vc.tabBarItem setBadgeValue:#"1"];
}
However, the badges images are shown clipped, like shown in here:
I think is because the badge is being added within the UITabBar's UIView, and it is clipping its contents. But I can't modify that value since that's a private property. What other options do I have here?
I haven't found a way to change the offset in the stock badges, so I had to roll out my own solution. It's not pretty, but it works (this is, again, on a UITabBarController category):
- (void) v_setBadgeValue:(NSString *)badgeValue inTab:(NSInteger)tabIndex
{
NSArray *viewControllers = [self viewControllers];
UIViewController *vc = [viewControllers objectAtIndex:tabIndex];
UITabBarItem *tabBarItem = vc.tabBarItem;
UIImage *normalImage = tabBarItem.image;
UIImage *selectedImage = tabBarItem.selectedImage;
UIImage *badgedNormalImage = [[self class] addBadgeWithValue:badgeValue toImage:normalImage];
UIImage *badgedSelectedImage = [[self class] addBadgeWithValue:badgeValue toImage:selectedImage];
[tabBarItem setImage:[badgedNormalImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
[tabBarItem setSelectedImage:[badgedSelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
}
+ (UIImage *) addBadgeWithValue:(NSString *)badgeValue toImage:(UIImage *)image
{
//Here you have to create your badge view. Let's just use a red square for now
UIView *badgeView = [[UIView alloc] initWithFrame:CGRectMake(16, 3, 14, 14)];
[badgeView setBackgroundColor:[UIColor redColor]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
[imageView addSubview:badgeView];
UIGraphicsBeginImageContextWithOptions((imageView.bounds.size), NO, [[UIScreen mainScreen] scale]);
[imageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return viewImage;
}
Now you're in complete control of the badges offset and position, that's important if you, like me, have a very strange UITabBar to work with. This is the result:

Change image in UIImageView

i have two ViewControllers, when i tap a cell i need to open other viewController and see image (image with names 1, 2, 3,...._full.jpg), so i write:
DetailViewController *dvc = [[DetailViewController alloc] init];
[dvc updateImage:[NSString stringWithFormat:#"%d", indexPath.row]];
[self.navigationController pushViewController:dvc animated:YES];
in my dvc
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor grayColor];
self.image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1024, 748)];
[self.image setTag:1];
[self.view addSubview:self.image];
}
method what i called
-(void)updateImage:(NSString*)imageName
{
[(UIImageView*)[self.view viewWithTag:1] setImage:[UIImage imageNamed:[NSString stringWithFormat:#"%#_full.jpg", imageName]]];
}
smth like self.image.image = [UIImage imageNamed:[NSString stringWithFormat:#"%#_full.jpg", imageName]]; didn't work for me.
so, these code works fine in my IOS 5,6,7 emulators, but when i compile it on my IPad 2, IOS 5.1 my image is not updating, all i see is gray background.
What am i doing wrong?
You run
[dvc updateImage:[NSString stringWithFormat:#"%d", indexPath.row]];
After that viewDidLoad is called and your image is reseted.
Instead that pass image name as a string
[dvc setImageName:[NSString stringWithFormat:#"%d", indexPath.row]];
and in DetailViewController.m in viewDidLoad call
[self updateImage:[NSString stringWithFormat:#"%#_full.jpg", self.imageName]];
Remember to add #property imageName to your DetailViewController.h file.
omg, XCode is joking, it's all because of my images where not ".jpg" they where ".JPG"!. I'm angry...

I want to hide button after clicking on action method

My Sample code:
- (IBAction)captureButton:(id)sender {
captureButton.hidden = YES;
currImage = [signatureViewController imageWithView:self.view];
NSLog (#"image %#",currImage);
imgView = [[UIImageView alloc]initWithFrame:CGRectMake(150,100,300,150)];
imgView.backgroundColor = [UIColor blackColor];
imgView.image = currImage;
[self.view addSubview:imgView];
image.image = nil;
}
My prob is capturebutton is geting hidden the view.. please help
Thanks in advance
Two options:
1) Add the following code to bring your image view to front:
[self.view bringSubviewToFront:imgView];
2) Hide your button:
[(UIButton *)sender setHidden:YES];
Try this,
yourButton.hidden = YES;
You could just try disabling the button.
If it is hidden behind a view this may help.
yourbutton.enabled = NO;

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.

Resources