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