image retrieved from url does not fit properly into UIImageView - ios

I'm trying to download an image from an url and display into a cell (sample below):
NSURL *url = [NSURL URLWithString:#"http://images.google.com/intl/en_ALL/images/logos/images_logo_lg.gif"];
NSData *data = [NSData dataWithContentsOfURL:url];
[cell.imageView setImage:[UIImage imageWithData:data]];
Somehow when I display into the cell, it covers the whole cell, bigger than my UIImageView.
But If I display the image locally like below:
[cell.imageView setImage:[UIImage imageNamed:#"defaultProfile.png"]];
It fits perfectly into the UIImageView I set into the cell.
Why is this the case?

use scaleToFit property of imageview

Related

UIImageView displays downloaded images from HTTP, but won't display image resource

In my first View Controller after a user logs in, there is a quick check to see if the user has uploaded a custom image to his account. (If he has, there's a URL in the user's profile data.)
These images are downloaded correctly in my code and they show up beautifully. The problem comes when there is no custom user image, and the VC tries to set the same UIImageView to a default picture (in xcassets). The picture is being loaded into the bundle, as far as I can tell, and it's a valid PNG file.
Here is the snippet for setting the image. If a custom URL is not found, I set the URL parameter string to "nil."
-(void) setImageWithUrl: (NSString *) Url Imageview: (UIImageView *) image {
if (Url.length > 4) {
NSURL *url = [NSURL URLWithString:Url];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *Profileimage = [UIImage imageWithData:data];
[image setImage:Profileimage];
}
else {
UIImage *Profileimage = [UIImage imageNamed:#"DefaultPicture"];
[image setImage:Profileimage];
}
}
This one's driving me nuts, so all weird ideas are welcome. :-)
Let me know if you want me to post any other parts of the code that you think could be a factor.
It looks like debug-need issue. But why don't you use any image download/cache library, like SDWebImage?
It gonna be much efficient and convenient

Cannot load an image from a remote url

I have tried many solutions for this problem before posting but somehow I didn't succeed.
I am using Xcode 6 and I am trying to add programmatically an image to my view. Everything is fine if I load an image beloging to the project, nothing works when I try to load an image resident on some remote server.
I have tried two different way:
1) using the libray SDWEBImage
UIImageView *imgg =[[UIImageView alloc] initWithFrame:CGRectMake(30, 250, 300, 20)];
NSURL *imageURL = [NSURL URLWithString:beaconLogo];
[imgg sd_setImageWithURL:imageURL placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
[self.view addSubview:imgg];
where beaconLogo is a string containing the url of the remote image (when I debug I can see that it has the correct value).
I have imported
#import "uiimageview+webcache.h"
#import "SDWebImageDownloader.h"
not sure if those are the correct headers needed. Anyway, the image is not displayed
2) I tried by using the following code that I found in some answer here on stackoverflow
NSURL *imageURL = [NSURL URLWithString:beaconLogo];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
UIImageView *imgg =[[UIImageView alloc] initWithFrame:CGRectMake(30, 250, 300, 20)];
imgg.image = image;
[self.view addSubview:imgg];
No image is displayed, however I can see that there is something bad going on, because if I debug, I can see that "image" is nil, this line
UIImage *image = [UIImage imageWithData:imageData];
doesn't seem to have the desider effect.
Where am I going wrong? Thanks in advance.

How can parse all the images form Website in IOS SDK

I like to implement image upload functionality in my app.For that I like to give some option for user to choose image.
One of the option is choose image from website.
If the user enter an url link in the text box and click the get images button means. I like to display all the images in that particular link
But I am not having any idea about this.
I have explain my requirement below
1)Enter the url in a text box
2)Click the get images button
3)Parse all the images from that particular url
4)Display all the images in the Imageview
And also to illustrate my requirement I have attached the screenshot below
If anybody work around this functionality means please suggest me some idea
Thanks
Like this ?
- (IBAction)buttonClicked:(id)sender
{
NSURL *url = [NSURL URLWithString:#"http://www.yourwebvsite.com/image.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData: data];
UIImageView *displayImageView = [[UIImageView alloc] initWithImage:image];
[displayImageView setFrame:CGRectMake(0, 0, 300, 300)];
[displayImageView setContentMode:UIViewContentModeScaleAspectFit];
[self.view addSubview:displayImageView];
}
Or if you want all the images from a web directory. See here and save all the images in an array then display the images in a UIImageView like my code above.

UIImageView ignores content mode form image data

I am trying load an image from a data :
NSError *error ;
NSString *imagePath = [[NSBundle mainBundle] pathForResource:imageName ofType:#"png"];
NSData *encryptedData = [NSData dataWithContentsOfFile:imagePath];
NSData *decryptedData = [RNDecryptor decryptData:encryptedData
withPassword:PASSWORD
error:&error];
UIImage *image = [UIImage imageWithData:decryptedData];
//adding image
UIImageView *movingImageView = [[UIImageView alloc]initWithImage:image];
[movingImageView setContentMode:UIViewContentModeScaleAspectFit];
[self.scrollView addSubview:movingImageView];
now movingImageView ignores content mode !!! it will be fixed if I declare its frame ! but the problem is my datas are different images with different width size . Any solution ?
EDIT :
Found a solution :
movingImageView.frame = CGRectMake(0, 0, image.size.width/2, image.size.height/2);
According to the documentation:
initWithImage: adjusts the frame of the receiver to match the size of the specified image. It also disables user interactions for the image view by default.`
Obviously the contentMode will have no effect if the imageView and image have the same size.
I guess what you need is to specify the imageView's width (probably the same as scrollView?), and calculate the height according to the image's dimension.
use the following lines. it will fill the image to frame. if large image came it will show image with aspect ratio and some part may clips. try once.
movingImageView.contentMode = UIViewContentModeScaleAspectFill;
movingImageView.clipsToBounds = YES;[self.scrollView addSubview:movingImageView];

Fastest way to load images into a UIView?

i have a UIScrollView which holds different UIView container. Each container has to load six UIImages. When scrolling i need to adjust the contents within the "layoutSubviews" method.
Unfortunately loading these images affects in hiccups when the new container and the images will be created.
I´m loading the images async via "dispatch_async". I also used small 32x32px thumbnails which will be replaced with the full image 256x256px image. Unfortunately its stuttering..
Here´s the code of the "initWithFrame" method of such a UIView.
These UIViews will later be added as a subview to the UIView container.
// load ThumbnailImage and replace it later with the fullImage
NSString* thumbImageName = [self.data.imageName stringByAppendingString:#"_small"];
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: thumbImageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSString* fileLocation = [[NSBundle mainBundle] pathForResource: self.data.imageName ofType:#"png"];
UIImage* image = [UIImage imageWithData:[NSData dataWithContentsOfFile:fileLocation]];
[self setBackgroundImage:img forState:UIControlStateNormal];
});
});
Is this the faster way to load images into a UIView?
How does Apple implemented the really fluid image view inside the "Photo.app" when scrolling between different fullscreen images?
I don´t know if it would make sense to use CoreImage?
Any advice would be great!
Thanks for any help and your time.
Have a nice day.

Resources