Need help programmatically setting a UIImageVIew Image - ios

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");
}
}

Related

UIImageView not showing in app

I have a UIImageView which is displayed using the following code:
profileImage = [self getImageFromUrl:#"http://i.imgur.com/l0HVdmR.png"];
if (profileImage == nil) {
NSLog(#"SourceImage is nil");
} else {
NSLog(#"SourceImage isn't nil");
}
profileImageView = [[UIImageView alloc] init];
profileImageView.image = profileImage;
[profileImageView setFrame:CGRectMake((screenWidth/2)-(profileDiameter/2), profileDiameter/2, profileDiameter, profileDiameter)];
[profileImageView setContentMode:UIViewContentModeScaleToFill];
profileImageView.userInteractionEnabled = isEditing;
NSLog(#"Online image used");
if (profileImageView == nil) {
NSLog(#"ImageView is nil");
} else {
NSLog(#"ImageView isn't nil");
}
Later on I use the code [mainScrollView addSubview:profileImageView to add the UIImageView to the UIScrollView I have set up. The UIImageView used to correctly display this image without any trouble. However, the UIImageView has suddenly stopped displaying altogether, and I'm unsure of what change elsewhere in my code caused this.
The framing isn't to blame. The variables are the correct values and changing them didn't solve the issue. The NSLogs show both the UIImage and UIImageView to not be nil. If it helps, the UITapGestureRecognizer I have set up on the UIImageView does not register taps in the area in which the image should be - This may provide some clue regarding the symptoms of the bug.
All help appreciated.
Check whether the value of profileDiameter was initialised or not. Also addSubview must be used.
Kindly upload some more details regarding how profileDiameter is found out.

UITableViewCell.ImageView.image Compare UIImage

I am comparing cell.imageView.image with UIImage as below code.
Please refer below Code
UIImage *imgOne = [UIImage imageNamed:#"One"];
if([customCell.imageView.image isEqual: imgOne]){
NSLog(#"Right Image");
}
While debugging, I write as po [customCell.imageView.image isEqual imgOne], but it always returns as 'nil'.
Can anyone tell me how to compare this or proper way to compare?
Generally UIImageView can not store file name of image but it store only UIImage so, it is not possible to get file name of image which you add in UIImageView.
Thats way if you want to compare only image file name then it is not possible to compare it.
But If you want to compare two UIImage object then
UIImage *secondImage = [UIImage imageNamed:#"image.png"];
NSData *imgData1 = UIImagePNGRepresentation(self.imageView.image);
NSData *imgData2 = UIImagePNGRepresentation(secondImage);
BOOL isCompare = [imgData1 isEqualToData:imgData2];
if(isCompare)
{
NSLog(#"Image View contains image.png");
}
else
{
NSLog(#"Image View doesn't contains image.png");
}
If you matain the "image one" as a member var.(1)
And set the cell's image this value.(2)
Then you can compare using those two pointers.(3)
See this code:
//1
self.imgOne = [UIImage imageNamed:#"One"];
//2
customCell.imageView.image=self.imgOne
//3
if(customCell.imageView.image == self.imgOne)
{}
Instead of Imageview I tried with Button
And than i Check as below
if([cell.button.currentImage isEqual: [UIImage imagenamed:"Your Imagenem"]]){
NSLog(#"Right Image");
}
It works fine..
Thank you.

My UIImage is loaded but doesn't appear on screen

i'm writing an app that changes an image through different conditions. When i debug everything is set but i do not see the image on my screen. Can anybody please help me?
-(void)setLuminanceImageSource
{
self.luminanceImageView.image = nil;
UIImage *image;
switch (self.luminanceImageSource) {
case FULL_LIGHT:
image = [UIImage imageNamed:#"belichting-goed"];
[self.luminanceImageView setImage:image];
NSLog(#"full");
break;
case HALF_LIGHT:
image = [UIImage imageNamed:#"belichting-matig"];
[self.luminanceImageView setImage:image];
NSLog(#"half");
break;
case BAD_LIGHT:
image= [UIImage imageNamed:#"belichting-slecht"];
[self.luminanceImageView setImage:image];
NSLog(#"bad");
break;
default:
image = [UIImage imageNamed:#"belichting-goed"];
break;
}
[self.luminanceImageView setImage:image];
[self.luminanceImageView setNeedsDisplay];
[self.view addSubview:self.luminanceImageView];
[self.view bringSubviewToFront:self.luminanceImageView];
}
This is my method that controls the image. I have an UIImageView in the .xib file and when i run the app i see the image i have selected in attribute inspector, but it disappears when this method is called.
EDIT: I have changed the names to the conventional naming style.
This is my debug-console, the image object is not nil but the luminanceImageView stays nil.
I have created a github repository where i cloned my project in so you can see my code.
CameraApp repository
Only difference is the location of the luminaceImageView, now it is located in the middle of the screen so i can be sure that it is in view of the device.
You say that:
the image object is not nil but the luminanceImageView stays nil.
Of course, you need to create the image view first, before you can set its image. The easiest in your case is probably to create it and set its image in one go, and then add it to your view:
self.luminanceImageView = [[UIImageView alloc] initWithImage:image];
[self.view addSubview:self.luminanceImageView];
EDIT (after looking at the GitHub):
You currently set the view like this:
self.luminanceImageView = [[UIImageView alloc] init];
[self.luminanceImageView setImage:image];
That means that if you set it in the xib (I have not checked that), then you override it here.
There are three problems with this:
the luminanceImageView still needs to be added to your view, as I
already stated above
the plain init creates an imageView with
size (0, 0). This is unlike the initWithImage:, which creates a imageView with the size of the image.
the setImage: does not resize the imageView. From the
UIWebView documentation:
Setting the image property does not change the size of a UIImageView. Call sizeToFit to adjust the size of the view to match the image.
If you have correctly set the imageView with its size in the XIB, then leave out everything else and just do:
[self.luminanceImageView setImage:image];
If the size is not correct, then follow this with:
[self.luminanceImageView sizeToFit];
EDIT (after debugging the code):
Your problem is that you set the image from a background thread. Almost all view operation in iOS need to be done from the main thread. Doing like this solves your problem:
dispatch_async(dispatch_get_main_queue(), ^{
[self.luminanceImageView setImage:image];
[self.view bringSubviewToFront:self.luminanceImageView];
});
BTW, you also change self.layer in your code. That is probably not a good idea and may lead to problems later. Use a separate view for the camera, and use [cameraView.layer addSublayer:newLayer] to get around that.
try this edit from your code
-(void)setLuminanceImageSource
{
[self.sess stopRunning];
//[self.layer removeFromSuperlayer];
UIImage *image;
switch (self.luminanceImageSource) {
case FULL_LIGHT:
{
image = [UIImage imageNamed:#"belichting-goed.png"];
NSLog(#"full");
break;
}
case HALF_LIGHT:
{
image = [UIImage imageNamed:#"belichting-matig.png"];
NSLog(#"half");
break;
}
case BAD_LIGHT:
{
image= [UIImage imageNamed:#"belichting-slecht.png"];
NSLog(#"bad");
break;
}
default:
{
image = [UIImage imageNamed:#"belichting-goed.png"];
break;
}
}
[self.luminanceImageView setImage:image];
[self.layer addSublayer: self.luminanceImageView.layer];
//[self.view bringSubviewToFront:self.luminanceImageView];
// NSLog(#"%#", self.luminanceImageView.image);
// NSLog(#"lumi X:%f, Y:%f", self.luminanceImageView.frame.origin.x, self.luminanceImageView.frame.origin.y);
// NSLog(#"view X:%f, Y:%f",self.view.frame.origin.x, self.view.frame.origin.y);
// NSLog(#"window %#", self.view.window);
}

show image in different image view when button clicked.

I've been trying to make this code work but can't really get final part together.
I got 2 rows of 6 image views,
Imageview 1 to 6 and 7 to 12.
First I click 6 buttons out of my 26 buttons (A-Z) and they display in the first 6 views with this code:
- (void)setImage:(UIImage *)image {
if (imageview1.image == nil) {
[imageview1 setImage:image];
} else if (imageview2.image == nil){
[imageview2 setImage:image];
} else if (imageview3.image == nil){
[imageview3 setImage:image];
} else if (imageview4.image == nil){
[imageview4 setImage:image];
} else if (imageview5.image == nil){
[imageview5 setImage:image];
} else if (imageview6.image == nil){
[imageview6 setImage:image];
}
}
-(IBAction)showA {
UIImage *img = [UIImage imageNamed:#"A.png"];
[self setImage:img];
}
now when I click the button play I want the images to change from view 1 - 6 to 7 - 12.
I hope there is someone who can explain me how to do this, I tried a couple of ways now but Can't really get it working, maybe making variable strings then get the image from that?
hope someone can explain me or has some sample code for me thanks
Like this?
[imageview7 setImage:imageView1.image];
[imageview8 setImage:imageView2.image];
etc...
[imageview1 setImage:nil];
[imageview2 setImage:nil];
etc...
It is not because you did not set up an image for your UIImageView that it did not alloc a space for the image.
In the other other way CGImage represents the "data of your image". If you did not set up the image but UIImageView alloc the UIImage for you, you are sure that the "data of this image" will be nil (because there is no image).
So you should try that out:
if([imageview1.image CGImage]==nil){[imageview1 setImage:image];}
Hope that help you

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

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"];

Resources