UIImageView not showing in app - ios

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.

Related

How do I change UIImageView image in view controller in traitCollectionDidChange

I want the image in my UIImageView to us a different image file that's cropped to correctly fill the landscape mode upon the orientation changing from portrait to landscape. All of the following code is defined in a class that extends UIViewController.
The following function is definitely being called when the phone rotates because my printf function is printing to the output.
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
|| (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
printf("Orientation Change!\n");
UIImage * newImage = [UIImage imageNamed: #"landscape-image"];
[self.imageView setImage:newImage];
}
}
I defined the property imageView in my .h file as follows:
#property (nonatomic, nonnull, readonly) UIImageView * imageView;
And I initialize the imageView as follows:
UIImage * image = [UIImage imageNamed:#"portrait-image"];
UIImageView * _imageView = [[UIImageView alloc] initWithImage:image];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
[pageView addSubview:_imageView];
This does not work for me though. When I change the orientation, the imageView image stays the same and zooms in like it normally does. Since the traitCollectionDidChange function is being called when the phone rotates, I assume the issue must be with how I'm changing the image. I'm relatively new to iOS development so I could just be missing something important for updating UIImageViews. Any help is appreciated.
When you create the imageView using UIImageView * _imageView = [[UIImageView alloc] initWithImage:image];, you are shadowing the automatically synthesised variable _imageView, with a new variable of the same name.
The newly created UIImageView instance is therefore not assigned to the imageView property. As a result, when the device is rotated and the second method is run, self.imageView is nil, and you call to [self.imageView setImage:newImage] does nothing.
To solve it, all you need to do replace UIImageView * _imageView = [[UIImageView alloc] initWithImage:image]; with _imageView = [[UIImageView alloc] initWithImage:image];
More on the messiness of automatically synthesised properties in Objective-C in this answer When should I use #synthesize explicitly?
EDIT --
In addition, the - (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection may be called at any time, and initially, so you want to detect the orientation in that method, and not just set the image to your landscape image.
(and on this - ou may want to re-evaluate your requirements regarding 'landscape' vs 'portrait' image because iOS apps can run in a variety of environment, iPad / iPhone / compact.. it is complicated. https://developer.apple.com/documentation/uikit/uitraitcollection)
To complete the answer, this should work on iPhone
- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {
[super traitCollectionDidChange: previousTraitCollection];
if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
|| (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
[self.imageView setImage:[UIImage imageNamed: #"landscape-image"]];
} else if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular
&& self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
[self.imageView setImage:[UIImage imageNamed: #"portrait-image"]];
}
}
}

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

UIScrollView stays put

I am following Paul Hegarty's 2012-2013 course in iTunes-u. In lesson 9 he gets the course to create a minimal app to display a photo on a UIScrollView. For him it worked first time. I thought I had copied the exercise exactly, but it has never scrolled for me.
The guts of code are this:
-(void)resetImage
{
if (self.scrollView) {
self.scrollView.contentSize = CGSizeZero;
self.imageView = nil;
UIImage *image = [UIImage imageNamed:#"photo_3.jpg"];
if (image) {
self.scrollView.contentSize = image.size;
self.imageView.image = image;
self.imageView.frame = CGRectMake(0, 0, image.size.width, image.size.height);
}
}
}
The image comes up and is fine in the simulator, but attempting to scroll has no effect.
Can anyone suggest why scrolling doesn't work?
TIA
Mark
I have the exact same issue. I tried everything but nothing worked for me. At last i have disabled the AutoLayout and it did start scrolling.
So just disable the AutoLayout and your ScrollView will start working.

Image comparison with button image

I am trying to compare an image from a button with another image but its not working as expected and as stackoverflow suggestions...
if (favoriteButton.imageView.image == [UIImage imageNamed:#"favorite.png"]) {
NSLog(#"yes!");
}
anything else I can do? I tried to get the image file name but it seems to be impossible.
UIImage is a class, not a native type. You should use the isEqual: method, not the == operator to see if the two objects represent the same data.
if ([favoriteButton.imageView.image isEqual:[UIImage imageNamed:#"favorite.png"]]) {
NSLog(#"yes!");
}
Only use the == operator with object pointers if you really want to see if the two pointer reference the exact same location in memory.
I found this: How to compare two UIImage objects
this is what I tink may help:
- (BOOL)image:(UIImage *)image1 isEqualTo:(UIImage *)image2
NSData *data1 = UIImagePNGRepresentation(image1);
NSData *data2 = UIImagePNGRepresentation(image2);
return [data1 isEqual:data2];
}
Reading the comments of your question, i think u cloud just assign the button a "state" such as "selected".
On the normal state the button cloud load the "not_favorite.png".If the user tap on the button it will be on a selected state and the corresponding image(favorite.png) will load.
On future if you need to know which image is loaded(or if the user already taped on the button to make it favorite) u can just check the button's state.
Personally i will not compare images just to fire an action for a button. It will be costly.
why not try to use imageView to identify the image?
UIImageView *imageView_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"favorite.png"]];
UIImageView *imageView_not_favorite = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"not_favorite.png"]];
//both imageView_favorite and imageView_not_favorite can be set as an member ivar, and the favoriteButton.imageView can be set to them appropriately.
if (favoriteButton.imageView == imageView_favorite) {
NSLog(#"yes!");
}
try this
must check your buttonname and extension. it must be same.
-(IBAction)btnMaleClick:(id)sender {
if (btnMale.imageView.image == [UIImage imageNamed:#"radiobtn.png"]) {
[btnMale setImage:[UIImage imageNamed:#"radiobtnselected.png"] forState:UIControlStateNormal];
[Female setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
}
else {
[btnMale setImage:[UIImage imageNamed:#"radiobtn.png"] forState:UIControlStateNormal];
[Female setImage:[UIImage imageNamed:#"radiobtnselected.png"] forState:UIControlStateNormal];
}
}

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