iOS sets the size of the Uiimageview based on the size of the network picture

Source: Internet
Author: User

Sometimes when you set the size of the Uiimageview, you need to automatically set it according to the uiimage aspect ratio, not to distort the original proportions of the picture.

If the picture is obtained from the local, [UIImage imagenamed:@ "];

This will get the image and get the size of the image. But most of the time we're on the web asking for pictures,

We need

NSData *data = [NSData Datawithcontentsofurl:url];

Image = [UIImage imagewithdata:data];

This gets the image, but in the case of a slow network, this synchronous request may cause the thread to get stuck.

In general, we use sdwebimage this third party to load the image. But all are used Uiimageview to tune sd_webimage such method directly set, not to get the image and then set the image of Uiimageview, So we can't get the size set to Uiimageview and set the image.

Find a way to solve this problem on the net, that is, there is a way to get the cache in Sdwebimage, first of all to determine whether Sdwebimage cache this image, some words to get, no re-use

NSData *data = [NSData Datawithcontentsofurl:url];

Image = [UIImage imagewithdata:data];

To get

Code as follows,

Fixed width when displaying a single picture.

__block cgfloat ITEMW = 200;

__block cgfloat itemh = 0;

if (_photodataarray.count = = 1) {

Uiimageview * ImageView = [[Uiimageview alloc] init];

Nsurl * url = [Nsurl urlwithstring:_photodataarray.firstobject];

[ImageView sd_setimagewithurl:url placeholderimage:[uiimage imagenamed:@ "Placeholder.jpg"];

Sdwebimagemanager *manager = [Sdwebimagemanager Sharedmanager];

BOOL Existbool = [Manager diskimageexistsforurl:url];//Determines if there is a cache

UIImage * image;

if (Existbool) {

image = [[manager Imagecache] imageFromDiskCacheForKey:url.absoluteString];

}else{

NSData *data = [NSData Datawithcontentsofurl:url];

Image = [UIImage imagewithdata:data];

}

Sets the height according to the scale of the image

if (image.size.width) {

ITEMH = Image.size.height/image.size.width * ITEMW;

if (ITEMH >= itemw) {

ITEMW = 120;

ITEMH = Image.size.height/image.size.width * ITEMW;

}

}

iOS sets the size of the Uiimageview based on the size of the network picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.