setImage method of UIImageView not working sometimes - ios

I have a UIImageView to show an image and a button to switch images from disk.
- (IBAction)loadNextImage:(id)sender {
if (m_currentPage >= m_pageCount - 1)
{
return;
}
m_currentPage++;
[self showImage:m_currentPage];
}
and the showImage method call the setImage method of UIImageVIew.
All this works well in simulator. But in my iphone4, for some certain images, it didn't show these images when I click the button to switch to these images. It still shows the last image. BUT When I make the app running in background and then switch it to foreground, the UI shows the image it should be!
I debuged it and the image parameter passed to setImage is not nil.
The image is about 200k. Is it too large? Is there anyone knows the reason?

Have you checked that the filenames match exactly what you are trying to load, i.e. image1.png is not the same as Image1.png as far as a device is concerned. But this will work in the simulator.

Related

Uiimageview not showing in iPhone 6 and 6 plus

I have collection view with custom cell.
And in custom cell iPhone 6 and 6 plus Uiimageview is not showing.
But when i am printing the frame its there.
NSLog(#"%f-%f-%f-%f",cell._image.frame.origin.x,cell._image.frame.origin.y,cell._image.frame.size.width,cell._image.frame.size.height);
Log -
0.000000-0.000000-177.500000-168.000000
Cell Screenshot is -
Any suggestions what i am doing wrong...
Note - its working properly in iPad, iPhone 4,4s,5.
Have you checked to see if the image is even loaded. For iPhone 6 (vs the working ones) it is looking for a file/resource of name ImageName#2x.png
Its also possible your image view is setting alpha to zero (so image ends up just being white as background). You may have something like this that you need to comment out (or switch on phone type) :
- (void) setAlpha:(float)alpha {
// [super setAlpha:alpha];
}
Where is your image loaded from? Maybe show that code too. Maybe you have offsets that put the image off the display?

Strange behaviour with iOS UIImageView and App going into background

I have a method, and in this method, there is this conditional:
if (self.sleepingCharacter.objectSprite.image == [UIImage imageNamed:#"sleepingRight.png"])
{....
This normally works fine. But I've noticed, in the iOS simulator, that when I put my App in the background by pressing command-H, and then i bring my app back, this conditional no longer works. Do you know why this would happen?
I tested to see if the code would work if I wrote this:
if (self.sleepingCharacter.objectSprite.image)
{....
And it did work, which means that there is still an image there. Now I am confused.
You are using the == operator to compare the two images. This will only be true of the two images are actually the same hunk of memory (the same pointer).
The UIImage imageNamed: method caches images. So in theory if you call it again and again for the same image name, you will keep getting the same pointer and your code appears to work.
But the image cache can get purged at times due to memory usage. Once the image gets purged, the next call to imageNamed: will return a new image pointer and your check will fail.
You need a better way to see if the two images are the same. One solution is to convert both images to NSData objects using UIImagePNGRepresentation then compare the two NSData objects using the isEqual: method.

UIImage looks different when used in iOS

I am having problems with a png image, that gets wrong colors on iOS compared to the actual image.
It does not matter how I am using the image, it always gets the wrong colors. I have tried on UIButton and UIImageView and it gives the same result.
It is a very standard use of a UIImage:
UIImage* greenButtonImg = [UIImage imageNamed:#"btn_green"];
UIImageView* testView = [[UIImageView alloc] initWithImage:greenButtonImg];
[self.view addSubview:testView];
The second image is how it looks on iOS and the first button is how it looks on my Mac (Finder and Photoshop):
As you can see the second button has a different green color.
This is happening all over the app where am using this picture. It happens in the Simulator and on a iPhone 5.
What can cause this issue? Can this be caused by settings in Photoshop, where the image was created?
As Jeff wrote in a comment it was a problem with the RGB Profiles.
I managed to fix the problem by converting the color profile in Photoshop:
Edit -> Convert to Profile... -> Set profile to "Apple RGB"
In iOS 7.0, the image is colored with the toolbar’s tintColor.
In iOS 7.0, all subclasses of UIView derive their behavior for tintColor from the base class.
By default, an image (UIImage) is created with UIImageRenderingModeAutomatic.
If you have UIImageRenderingModeAutomatic set on your image, it will be treated as template or original based on its context.
Certain UIKit elements—including navigation bars, tab bars, toolbars, segmented controls—automatically treat their foreground images as templates, although their background images are treated as original.
Other elements—such as image views and web views—treat their images as originals. If you want your image to always be treated as a template regardless of context, set UIImageRenderingModeAlwaysTemplate.
If you want your image to always be treated as original, set UIImageRenderingModeAlwaysOriginal.
Refer Template Images for more info.

Image invisible in iPad because wrong Xcode folder management

I have small problem. When I launch my app in emulator it works just fine, but when I launch it on my iPad I cant see the images of the buttons. The background is OK. Image 1.jpg is shown, but images in General folder like contour buttons is invisible on iPad. Other images like Glasses.png that in other screen are shown fine also. The image 1.jpg is defined via code:
- (void)viewDidLoad
{
[super viewDidLoad];
[mainImgae setImage:[UIImage imageNamed: #"Iview images/Bike/100/comp/1.jpg"]];
}
And other images for buttons selected via storyboard. I think my folder management is wrong, but i have to manage it in folders, so they will be in folders at my project folder.
[mainImgae setImage:[UIImage imageNamed: #"1.jpg"]];
this is enough for putting image for mainimage.,.

Loading images into memory in iPhone App

I have an UImageView and on click of an button i want to change the UImage in the UImageView. App has 22 UImages with each image exceeding 5 mb size , so the UImageView takes some time to load an image when the button is clicked , is there a way by which we can load these images into the memory so that image view takes lesser time to show the image?
I use the following code to set the image :
[ImageView setImage:[UIImage imageNamed:#"ImageName.jpg"]];
basically i want on swipe the images to be changed , e.g. if a user swipes from right to left the app must show the next image and on left to right click the back image be loaded . please suggest .
like #Gobot mentioned it is the best way du reduce the size of your pictures. For the swipe there is no need to use big pictures like this. You could just create two folders, one with small sized pictures and another with bigger one. If you swipe through, you just use the small pictures. If a user taps a picture you could get the name of the selected UIImage and load just this single picture. That way you also can provide high quality pictures for zooming in. And keep in mind, that your app package gets also bigger the more pictures you have included.
There is a good explanation in a WWDC Stream: iOS App Performance: Graphics and Animations
I also created a method inside a NSMutableArray category for loading all my UIImages once. You could use this method inside viewDidLoad:
- (NSMutableArray *)getImagesWithStartSuffix:(NSString *)start
andEndSuffix:(NSString *)end{
NSMutableArray *imageArray = [[NSMutableArray alloc] init];
for (int i = 1; i < 100 + 2; i++) {
NSString *fileName = [NSString stringWithFormat:#"%#%d%#.jpg",
start, i, end];
if([self fileExistsInProject:fileName]){
[imageArray addObject:[UIImage imageNamed:fileName]];
} else {
break;
}
}
return imageArray;
}
There is also a very easy way to change the size of your pictures by writing an Apple Script. You could run a loop over all pictures and resize them all this way. This is the easiest way and you don't have to use any tools like Gimp or PS:
do shell script "sips [path_to_file]/picture1.jpg -z 666 1000 --out [path]/changedSize/picture1_small.jpg"
From the Documentation:
This method looks in the system caches for an image object with the specified name and returns that object if it exists. If a matching image object is not already in the cache, this method loads the image data from the specified file, caches it, and then returns the resulting object.
if it takes too long, you may want to add a progress indicator in your app till the full view is generated/loaded then display it on your screen.
The first and simplest option would be to reduce your images under 5MB. Do they really need to be that big? Are they thumbnails? The more complex option would be to load/cache images in a background thread (asynchronously) using dispatch queues.

Resources