How can I change the image displayed in a UIImageView programmatically? - ios

I have an IBOutlet to a UIImageView, but when I look at the UIImageView doc, I can't see any hints about programmatically changing it. Do I have to fetch an UIImage object from that UIImageView?

If you have an IBOutlet to a UIImageView already, then all you have to do is grab an image and call setImage on the receiver (UIImageView). Two examples of grabbing an image are below. One from the Web, and one you add to your Resources folder in Xcode.
UIImage *image = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://farm4.static.flickr.com/3092/2915896504_a88b69c9de.jpg"]]];
or
UIImage *image = [UIImage imageNamed: #"cell.png"];
Once you have an Image you can then set UIImageView:
[imageView setImage:image];
The line above assumes imageView is your IBOutlet.
That's it! If you want to get fancy you can add the image to an UIView and then add transitions.
P.S. Memory management not included.

Note that the NIB file doesn't wire up all the IBOutlets until the view has been added to the scene. If you're wiring things up manually (which you might be doing if things are in separate NIBs) this is important to keep in mind.
So if my test view controller has an "imageView" wired by a nib, this probably won't work:
testCardViewController.imageView.image = [UIImage imageNamed:#"EmptyCard.png"];
[self.view addSubview:testCardViewController.view];
But this will:
[self.view addSubview:testCardViewController.view];
testCardViewController.imageView.image = [UIImage imageNamed:#"EmptyCard.png"];

This worked for me
[ImageViewName setImage:[UIImage imageNamed: #"ImageName.png"]];
Make sure that the ImageView is declared properly in the .h file and is linked with the IB element.

imageView.image = [UIImage imageNamed:#"myImage.png"];

For the purpose of people who may be googling this to try to solve their problem, remember to properly declare the property in your header file and to synthesize the UIImageView in your implementation file... It'll be tough to set the image programmatically without getter and setter methods.
#import <UIKit/UIKit.h>
#interface YOURCONTROLLERNAME : UIViewController {
IBOutlet UIImageView *imageToDisplay;
}
#property (nonatomic, retain) IBOutlet UIImageView *imageToDisplay;
#end
and then in your .m :
#implementation YOURCONTROLLERNAME
#synthesize imageToDisplay;
//etc, rest of code goes here
From there you should be fine using something like the following to set your image.
[YOURCONTROLLER.imageToDisplay setImage:[UIImage imageNamed:value]];

Example in Swift:
import UIKit
class ViewController: UIViewController {
#IBOutlet var myUIImageView: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
}
#IBAction func myAction(sender: UIButton) {
let newImg: UIImage? = UIImage(named: "profile-picture-name")
self.myUIImageView.image = newImg
}
#IBAction func myAction2(sender: UIButton) {
self.myUIImageView.image = nil
self.myUIImageView.image = UIImage(data: NSData(contentsOfURL: NSURL(string: "http://url/image.png")!)!)
}
}

Following Jordan's advice (which should work actually), try to set the UIImageView to be visible:
[imageView setHidden: NO];
and also - don't forget to attach it to the main UIView:
[mainView addSubview: imageView];
and to bring to the front:
[mainView bringSubviewToFront: imageView];
Hope combining all these steps will help you solve the mystery.

My problem was that I tried to change the image in an other thread. I did like this:
- (void)changeImage {
backgroundImage.image = [UIImage imageNamed:#"img.png"];
}
Call with:
[self performSelectorOnMainThread : #selector(changeImage) withObject:nil waitUntilDone:NO];

Don't forget to call sizeToFit() after you change image if you then use size of UIImageView to set UIScrollView contentSize and/or compute zoom scale
let image = UIImage(named: "testImage")
imageView.image = image
imageView.sizeToFit()
scrollView.contentSize = imageView.bounds.size

If you want to set image to UIImageView programmatically then Dont Forget to add UIImageView as SubView to the main View.
And also dont forgot to set ImageView Frame.
here is the code
UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 460)];
myImage.image = [UIImage imageNamed:#"myImage.png"];
[self.view addSubview:myImage];

myUIImageview.image = UIImage (named:"myImage.png")

Working with Swift 5 (XCode 10.3) it's just
yourImageView.image = UIImage(named: "nameOfTheImage")

UIColor * background = [[UIColor alloc] initWithPatternImage:
[UIImage imageNamed:#"anImage.png"]];
self.view.backgroundColor = background;
[background release];

This question already had a lot of answers. Unfortunately none worked for me.
So for the sake of completenes I add what helped me:
I had multiple images with the same name - so I ordered them in sub folders. And I had the full path to the image file I wanted to show. With a full path imageNamed: (as used in all solutions above) did not work and was the wrong method.
Instead I now use imageWithContentsOfFile: like so:
self.myUIImage.image = [UIImage imageWithContentsOfFile:_currentWord.imageFileName];
Don't know, if anyone reads that far?
If so and this one helped you: please vote up.
;-)

To set image on your imageView use below line of code,
self.imgObj.image=[UIImage imageNamed:#"yourImage.png"];

Related

how do i add a UIView controller to a UIView

i have a custom class of type UIViewController. i want to add it to a UIView so that it can be used to output to the screen.
the UIView is called engineView
my custom UIViewController is called Engine. there is a custom method in the controller called addImage.
the code is as follows:
Engine *engine = [[Engine alloc] init];
CGRect frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
engine.view.frame = frame;
UIImage *image = [UIImage imageNamed:#"photo2.jpg"];
[engine addImage:image];
[engineView addSubview: engine.view];
this does not display the image.
however if i add the view controller through storyboard, it seems to work (if i add the image at the viewdidlayoutsubviews method. but i would like to call functions to it programically from the parent UIViewCcontroller.
can anyone tell me how i do this?
You can set storyBoard identifier in interface builder and ca use it like suggested above.
MyViewController *instance = [self.storyBoard instantiateViewControllerWithIdentifier:#"yourIdentifier"];
As far as adding a ViewController to UIView you can achieve that by using addChildViewControllermethod too.
try this..
Engine *eng=[self.storyboardinstantiateViewControllerWithIdentifier:#"YourStoryboradId"];
*you can set storyboard id by going in Identity Inspector.
After that You can add image on your View Controller by following code
UIImageView *image=[UIImageView alloc]initWithFrame:CGRectMake(x, y, your width, your Height);
img setImage:[UIImage imageNamed:#"imageName"];
[eng addSubview:image];
try like this..
Engine *engine = (Engine *)[self.storyboard instantiateViewControllerWithIdentifier:#"EngineViewControllerStoryBoardID"];
to set the story board id
to pass an image to Engine:
-->create a UIImage property in Engine.h
like #Property UIImage * engineImage;
--> assign that image to UIIMageView in Engine.m
-(void)viewWillAppear:(BOOL)animated
{
engineImageView.image = engineImage; // engineImageView is an example UIImageView replace it with your UIImageView
}
--> now send the image like
engine.engineImage =[UIImage imageNamed:#"photo2.jpg"];

Need help programmatically setting a UIImageVIew Image

I want to do is set the image of an empty UIImageView but whatever I try, nothing seems to work. Sorry if this is stupid, I am new to Xcode and Obj-C. The UIImageView takes up whole of the view, and there is a button to load the image (I have also tried putting this in the viewDidLoad bit, no success). Here is the code from my ViewController.m:
- (IBAction)buttonChangeImage:(id)sender {
UIImage *image = [UIImage imageNamed:#"1.jpg"];
[_imageView setImage:image];
}
Thanks in advance
Probably you did not connect IBOutlet _imageView with imageView in XIB or Storyboard.
Just in case you're having trouble interpreting #djromero's comment, here's how you could see if your problem is with the image:
- (IBAction)buttonChangeImage:(id)sender {
UIImage * imageToAdd = [UIImage imageNamed:#"1.jpg"];
if (imageToAdd) {
_imageView.image = imageToAdd;
NSLog(#"Image exists, if still not working, make sure _imageView is set properly");
}
else {
NSLog(#"There was a problem loading your image, make sure it is named properly");
}
if (_imageView) {
NSLog(#"ImageView exists!");
}
else {
NSLog(#"ImageView does not exist, make sure it is properly connected");
}
}

Setting an background image over an image set for a UIImageView - Objective C

I am setting images to a UIImage programmatically. But I want to also add a play button over that view. Think of Facebook in how there is a play button over the image. That is exactly what I am trying to figure out on what to do. Here is my setting of the UIImage:
UIImage *img = [UIImage imageNamed:[NSString stringWithFormat:#"%d.jpg", indexPath.row]];
cell.photoView.image = img;
Suggestions, thoughts?
You can add playbutton either as a UIButton or a UIImageView and add as a subview to your photoview.
[cell.photoView addSubview:urPlayButtonView];

iOS app - Apply background image object to UIView

Can anyone help me understand how I apply a background image object to a UIView please?
I have created a background image which is a blurred version of a background and I would like to apply it to be the background of a uiView in the foreground which would ideally mask the background image.
I have the following code so far -
_blurImage = [source stackBlur:50];
[_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:[_blurImage]]];
I would like to apply the image object(_blurImage) to be the background image of _hpBlurView but i'm struggling to get it working!
At first glance, you are using too many brackets. Here is a working version of your code :
_burImage = [source stackBlur:50];
_HPBlurImage.backgroundColor = [UIColor colorWithPatternImage:_blurImage];
I can't see what stackBlur:50 returns. So start from the beginning. colorWithPatternImag takes UIImage as a parameter. So Start by adding a picture, any picture, to your application. Lets imagine that the image is called image.png. This is one way to do it:
UIImage *image = [UIImage imageNamed:#"image.png"];
_HPBlurView.backgroundColor = [UIColor colorWithPatternImage:image];
This should help to get you going.
Create an image and add to the background.:
UIImage *image = [UIImage imageNamed:#"youimage"];
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
It's that.
To make sure everything resizes properly, no matter rotation, device size and iOS version, I just set an UIImageView
//Create UIImageView
UIImageView *backgroundImageView = [[UIImageView alloc] initWithFrame:self.view.frame]; //or in your case you should use your _blurView
backgroundImageView.image = [UIImage imageNamed:#"image.png"];
//set it as a subview
[self.view addSubview:backgoundImageView]; //in your case, again, use _blurView
//just in case
[self.view sendSubviewToBack:backgroundImageView];

How can I display a UIImage in a UIImageView?

I am playing around with Xcode 5 and storyboarding. I'm at the point where I have captured a UIImage using the camera, but I can't figure out how to display the image on the phone after I've captured it. I have specified an IBOutlet UIImageView and set it's value to the image that I captured from the camera, but that doesn't seem to do anything at all.
This is my interface:
#interface HCViewController ()
#property (nonatomic, retain) IBOutlet UIImageView *image;
#end
And this is my didFinishPickingWithMediaInfo method:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the picture from the camera
UIImage *imageFromCamera = [info objectForKey:UIImagePickerControllerEditedImage];
// Set the IBOutlet UIImageView to the picture from the camera
// This does nothing as far as I can tell
self.image = [[UIImageView alloc] initWithImage:imageFromCamera];
// Let's take a closer look at this
NSLog(#"%#", self.image);
// Close the camera view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
This is what I see in the logger for self.image:
2013-09-10 17:17:45.169 YourMom[6136:60b] <UIImageView: 0x1556fdc0; frame = (0 0; 0 0); userInteractionEnabled = NO; layer = <CALayer: 0x155d5b10>>
My storyboard has a View Controller with two different scenes that can be swiped back and forth. Both of the scenes have a "View" with an "Image View" as a sub-item. The Image Views each have "Referencing Outlets" that seem to be connected to the image variable that I defined in my interface. However, simply setting the value of image doesn't change the phone display.
After reading this SO question: How can I change the image displayed in an UIImageView programmatically? I tried [self.image setImage:image], but that didn't appear to do anything either. How do I tell Xcode that I want image to show up in the view?
Your mistake is in this:
self.image = [[UIImageView alloc] initWithImage:imageFromCamera];
Because you are using Storyboard your UIImageView will be initialized for you. By executing this line of code you are throwing away an old UIImageView and replacing it with a new UIImageView that has no frame.
You just need to do this:
[self.image setImage:imageFromCamera]
Also, you might be getting the wrong image from info. Try UIImagePickerControllerOriginalImage instead of UIImagePickerControllerEditedImage.
Hope this is helpful, cheers!
UIImage View has an property called image.
This property is of type UIImage.
So if you have an UIImage, then you can set the property as follows:
imageView.image = image;
// this is your delegate method to set the image taken from your camera
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
// Get the picture from the camera
UIImage *imageFromCamera = [info objectForKey:UIImagePickerControllerEditedImage];
// alloc image view as you have to set it image on it
UIImageView *imgView = [[UIImageView alloc]init];
imgView.image = imageFromCamera;
// you can also set the image on button like this
UIImage *imageFromCamera = [info objectForKey:UIImagePickerControllerEditedImage];
// imgBtn is UIButton property
[self.imgBtn setImage:imageFromCamera forState:UIControlStateNormal];
// Let's take a closer look at this
NSLog(#"%#", self.image);
// Close the camera view controller
[self dismissViewControllerAnimated:YES completion:nil];
}
You can try this code to display your image.
imageView.image : UIImage = UIImage(named: "YourImageName")
Hope this can help you with your problems.

Resources