UIImageView set to imageWithData ignores contentMode - ios

In my custom tableViewCell, I have an imageView (not the tableViewCell's default imageView) which fills the entire cell. It is set up in IB (and by code) with the contentMode UIViewContentModeAspectFill, and it clips to bounds. Whenever I change the imageView's image to an image loaded with [UIImage imageNamed:...], it resizes and fits the imageView as wished and expected. However, when the image set is loaded with [UIImage imageWithData:...], the image is set, but not resized.
The code that loads the image with data itself is run in a background thread, and looks like this:
- (void)getImage:(NSString *)URL {
NSError *error = nil;
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:URL] options:NSDataReadingMapped error:&error]];
if (!error){
[self.thumbView setImage:image];
[self.thumbView setContentMode:UIViewContentModeScaleAspectFill];
}
}
I have tried setting the image on the main thread, in layoutSubviews, but it always produces the same result – it doesn't resize to fill the imageView. I added the image (JPG) to the app bundle and set it programmatically with [UIImage imageNamed:...], which works, but the app has to get the data from a URL.
I also tried using the UIImageView+AFNetworking-class to set the image async instead of making my own thread, but that doesn't work either.
Why isn't the image respecting the UIImageView's contentMode when it's loaded from data? Any help is appreciated. Neither JPEGs nor PNGs are working.

If anyone else stumbles upon the same issue, here's what I did to fix it.
I made a new imageView in IB, removed the old one, and only changed the contentMode-value and clipsToBounds-value. In the tableViewCell.m init-code, I set the placeholder image, and voila – everything works like a charm.
Although I don't really know what caused it, it may have been the Clears Graphics Content checkmark's fault for being set (oops).

Related

Get size of image loading through URL

In my application I am displaying feed in tableview. Image is coming through URL. I want to calculate cell height dynamically based on image size. But I get image after cell get load.
How to achieve dynamic height of cell based on image size?
I hope my question is clear and I will get solution here.
Thanks,
When your images are loaded, you can calculate all heights for your rows, and if your heightForRowAtIndexPath method is implemented respectively just call [tableView reloadTable].
The simplest way would be to get the image sizes in the api itself if the service is handled by you itself. Since without getting the image downloaded you will not get its size.
Other way would be to keep the imageview in table row in a constant size and aspect fill the image inside it as most of the applications do.
Try this code:
But you can use image downloading dispatch_async.
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://static.hugedomains.com/images/logo_huge_domains.gif"]];
UIImage *image = [UIImage imageWithData:imageData];
NSLog(#"image height: %f",image.size.height);
NSLog(#"image width: %f",image.size.width);

UIImage not displayed

I am using iOS 7 SDK and Xcode 5. I want to set icons on UITabBarItem. I am using below method.
setFinishedSelectedImage: withFinishedUnselectedImage:
It was working till now. But suddenly it stopped displaying images.
I tried below various options :
[img imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
[tabBarItem setSelectedImage:img];
[tabBarItem setImage:img];
[[UITabBarItem alloc] initWithTitle:title image:img selectedImage:img];
None of them is working. Here, img is the image downloaded from URL.
Then I tried with one of the images in app resource and it was displaying.
After that, I tried using temporary UIImageView with background color to red and set img to this UIImageView, it displayed red color instead of img.
But strange part is that I use the same img in other view controllers where it is displayed correctly.
Any idea how to solve this issue?
It was my mistake in code. I had to initialze img to some UIImage on declaration.
Below is shown what was the issue.
UIImage * img; // Image not initialized
// This condition was not satisfied and hence `img` was not storing any time.
if (condition)
{
img = ...;
}
// Below condition should have been satisfied if `img` was not initialized.
// But it did not. I am not getting why.
if (!img)
{
NSLog(#"Image is nil");
}
[tabBarItem setImage:img];

UIImageView+animatedGIF always LOOPS

I used class made by "mayoff" (Rob Mayoff) "UIImageView+animatedGIF" which was proposed in one of the answers here on stackoverflow. UIImageView+animatedGIF
With it I can import animated .gif images in UIImageView on iOS. This class works flawlessly, but the only problem is that .gif is always looping. No matter how I export it (I am exporting image from photoshop - 67 frames, set repeat to "once") it loops forever in UIImageView.
I am importing my .gif with these two lines:
NSURL *url = [[NSBundle mainBundle] URLForResource:#"Loading" withExtension:#"gif"];
self.loadingImageView.image = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
very simple and it works.
I have tried setting animationRepeatCount property to 1, as recomended by Rob but that didn't do the trick. Also I have tried setting the UIImageView's animationImages property to
gifImage.images too, instead of just setting the view's image property
to gifImage. In that case, .gif is not animating at all.
Any ideas how to play that .gif only once? I can think of many tricks (like setting last frame of the gif to show up after some time but I'd first try something simpler if possible).
I took the test app from that project and changed the viewDidLoad method to this:
- (void)viewDidLoad {
[super viewDidLoad];
NSURL *url = [[NSBundle mainBundle] URLForResource:#"test" withExtension:#"gif"];
UIImage *testImage = [UIImage animatedImageWithAnimatedGIFData:[NSData dataWithContentsOfURL:url]];
self.dataImageView.animationImages = testImage.images;
self.dataImageView.animationDuration = testImage.duration;
self.dataImageView.animationRepeatCount = 1;
self.dataImageView.image = testImage.images.lastObject;
[self.dataImageView startAnimating];
}
When the app launches, it animates the image once and then shows just the last frame. If I connect a button that sends [self.dataImageView startAnimating], then tapping the button runs the animation one more time, then stops again on the last frame.
Make sure you're setting your image view up the way I do above. Make sure you are not setting the image view's image property to the animated image. The image view's image property must be set to a non-animated image if you want the animation to stop.
I have forked "uiimage-from-animated-gif" made by "Dreddik" and added a delegate method
#pragma mark - AnimatedGifDelegate
- (void)animationWillRepeat:(AnimatedGif *)animatedGif
{
NSLog(#"animationWillRepeat");
//[animatedGif stop];
}
So you know the time when animation will repeat and do your own thing in middle of every repetition. You may count the number of times animation repeated and stop animation at a certain number reached.
The forked repository is at https://github.com/rishi420/Animated-GIF-iPhone
Was having issues in start/stp animation & managing animation count
So used this pod
imageViewObject.animate(withGIFNamed: "gif_name", loopCount: 1) {
print("animating image")
}
To start/stop animation :
imageViewObject.isAnimatingGIF ? imageViewObject.stopAnimatingGIF() : imageViewObject.startAnimatingGIF()

iOS UIImage stretching: How to fit a larger UIImage in a smaller UIImageView without any quality decrease and without any spaces

![enter image description here][1]I am taking a UIImage from UIImagePickerController and showing it in a UIImageView. Actually the UIImagePickerController is in full screen and thus the UIImage is also. But UIImageView is not in full screen because of presence of a header bar.
The Image is getting stretched..
How do I fix this..
Please help me someone..
THANKS IN ADVANCE.....
First image is the image picker screen and second one is where I am displaying the UIImage captured from picker in a UIImageView.
![This is my UIImagePickerController][2]
![This is the image after capturing being shown in a UIImageview. ][3]
NOTE: BOTH THE SCREENS ARE IN LANDSCAPE MODE
Use this for your UIImageView
imageView.contentMode = UIViewContentModeScaleAspectFill;
You won't get any space and with scale preserved. However, some part of the image will be clipped off.
If you use:
imageView.contentMode = UIViewContentModeScaleAspectFit;
There will be some empty space, but scale is preserved.

How to retrieve image locally from application and display it on UIImageView with Objective-C

I have loaded the image in application's folder that is source
but I could not find the code by which I will display that image
on UIImageView using button click event. Please help me out for this.
I have used this following code before:
UIImage *img = [UIImage imageNamed:#"xyz.png"]
but I could not set the UIImageView image to img in the method
Then please help me with code to set the UIImageView image to img in the method I call on my button click.
You can get an image added to your project folder/subfolder using
UIImage *img = [UIImage imageNamed:#"xyz.png"]
Then set the UIImageView image to img in the method you call on your button click.
Drag the image into Xcode and then set up an UIImage variable like this:
UIImage *theImage = [UIImage imageNamed:#"theImage.png"];
then implement it into your UIImage view by 'myImageView.image = theImage;
Make sure you have IBOutlet set up to your image view, though. The easiest way is to just drag the image into Xcode and select it under the popup list in Interface Builder.

Resources