My UIImage is loaded but doesn't appear on screen - ios

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

Related

Change image when a button is clicked using Objective-C

I am trying to switch between two different images when clicking on a button. Below is the code I wrote so far. However, this will only display one image and not toggle between them.
-(IBAction)show {
BOOL img = true;
if (img = true) {
UIImage *img1 = [UIImage imageNamed:#"UnCheck.png"];
[imageview setImage:img1];
}
else {
UIImage *img = [UIImage imageNamed:#"Check.png"];
[imageview setImage:img];
}
}
You have a couple of issues with your code:
First, img is a local variable inside your tap handler, so each time that function runs, it will be set to true.
Second, your if statement actually contains an assignment (=) instead of a comparison (==), so even if img wasn't already true it would be when you execute the if statement.
All of this means that your image is always going to be UnCheck.png.
You need to use a property, outside the function, so that the state is tracked properly. img is also a pretty poor variable name, checked or isChecked is probably better.
Then your button handler method simply needs to toggle this property and set the appropriate image.
#property BOOL isChecked;
-(IBAction)show {
self.isChecked = !self.isChecked;
NSString *imageName = self.isChecked ? #"Check.png":#"UnCheck.png";
UIImage *img = [UIImage imageNamed:imageName];
[imageview setImage:img];
}

When repeating image in image view its display small small slot in iOS

In my application want to repeat image in imageview but when i repeat image small small slot display how to resolved??
I tried below code :
self.bgimg.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed: #"pattern-dots"]];
Because your image size is smaller than the view size.
So, you provide enough larger image than self.view size.
You can try to check by bellow code fragment.
UIImage *image = [UIImage imageNamed: #"pattern-dots"];//#"oval"
if (self.view.frame.size.width<=image.size.width) {
self.view.backgroundColor = [UIColor colorWithPatternImage:image];
}else{
NSLog(#"You image is too small to fit.");
}
NSLog(#"view Size:%#",NSStringFromCGSize(self.view.frame.size));
NSLog(#"imsge Size:%#",NSStringFromCGSize(image.size));

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

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

iOS adding subviews to a scroll view

I'm using a double for loop to add UIButtons to a UIScrollView in a grid format. These UIButtons take time to load as they have subviews that are UIImageViews which get their UIImages by downloading data off the internet.
Right now, the subviews don't show until AFTER the method completely finishes executing. Correct me if I'm wrong, but I'm guessing xcode doesn't show added subviews until a method is done executing.
However, I do want to show each subview getting added one at a time, as a cool loading effect. How would I implement this?
Thanks!
You should use multiple threads to load your pictures so that your main thread does not become sluggish. I recently wrote something similar...Take a look at my code from my viewWillAppear method:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
self.myImages = [self.myModel retrieveAttachments]; //Suppose this takes a long time
for (UIImage *image in self.myImages)
{
dispatch_async(dispatch_get_main_queue(), ^{
[self addImageToScrollView:image animated:YES]; });
}
}
});
The addImageToScrollView method would be like so:
-(void) addImageToScrollView: (UIImage *) image animated: (BOOL) animated
{
//Create image view
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
imageView.image = image;
if(animated)
{
imageView.alpha = 0;
[self.myScrollView addSubview:imageView];
[UIView animateWithDuration:ADD_IMAGE_APPEARING_ANIMATION_SPEED animations:^{
imageView.alpha = 1;
}];
}
else
{
[self.myScrollView addSubview:imageView];
}
}

Resources