Cat Share, must boutique
Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243
: One effect
If the direct setting will have the condition of stretching and so on, here mainly introduces some details of the picture display
Two: Code
The code implementation is actually very simple, the micro-blog used a photos to store nine these pictures, and then use a photo class to do each photo, and the above display GIF and other styles, a lot of small tricks, directly on the code
Nine Gongge based on the row and column setting algorithm, not difficult
#import "HWStatusPhotosView.h" #import "HWPhoto.h" #import "HWStatusPhotoView.h" #define HWSTATUSPHOTOWH#define Hwstatusphotomargin#define HWSTATUSPHOTOMAXCOL (Count) ((count==4)? 2:3) @implementation hwstatusphotosview //9- (ID) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self) { }return Self;} - (void) Setphotos: (Nsarray*) photos{_photos = photos;intPhotoscount = Photos. Count;//Create a sufficient number of picture controls //The self.subviews.count here do not assign to other variables individually while( Self. Subviews. Count< Photoscount) {Hwstatusphotoview *photoview = [[Hwstatusphotoview alloc] init]; [ SelfAddsubview:photoview]; }//Traverse all the picture controls, set the picture for(inti =0; i< Self. Subviews. Count; i++) {Hwstatusphotoview *photoview = Self. Subviews[i];if(I < Photoscount) {//DisplayPhotoView. Photo= Photos[i]; PhotoView. Hidden=NO; }Else{//HidePhotoView. Hidden=YES; } }}- (void) layoutsubviews{[SuperLayoutsubviews];//Set the size and position of the picture intPhotoscount = Self. Photos. Count;intMaxcol = Hwstatusphotomaxcol (Photoscount); for(inti =0; i<photoscount; i++) {Hwstatusphotoview *photoview = Self. Subviews[i];intCol = i% Maxcol; PhotoView. x= Col * (hwstatusphotowh + hwstatusphotomargin);introw = I/maxcol; PhotoView. Y= Row * (HWSTATUSPHOTOWH + hwstatusphotomargin); PhotoView. Width= HWSTATUSPHOTOWH; PhotoView. Height= HWSTATUSPHOTOWH; }}+ (cgsize) Sizewithcount: (int) count{//Maximum number of columns (maximum number of rows in a row) intMaxcols = Hwstatusphotomaxcol (count);intcols = (count >= maxcols)? Maxcols:count;CGFloatPHOTOSW = cols * hwstatusphotowh + (cols-1) * Hwstatusphotomargin;//number of rows introws = (count + maxcols-1)/Maxcols;CGFloatPhotosh = rows * hwstatusphotowh + (Rows-1) * Hwstatusphotomargin;returnCgsizemake (PHOTOSW, Photosh);}@end
Photo's Code
#import "HWStatusPhotoView.h" #import "HWPhoto.h" #import "uiimageview+webcache.h" @interface hwstatusphotoview()@property(nonatomic,Weak)Uiimageview*gifview;@end @implementation hwstatusphotoview - (Uiimageview*) gifview{if(!_gifview) {UIImage*image = [UIImageimagenamed:@"Timeline_image_gif"];Uiimageview*gifview = [[UiimageviewAlloc] initwithimage:image]; [ SelfAddsubview:gifview]; Self. Gifview= Gifview; }return_gifview;} - (ID) initWithFrame: (CGRect) frame{ Self= [SuperInitwithframe:frame];if( Self) {//Content mode Self. Contentmode= Uiviewcontentmodescaleaspectfill;//out-of-border content is cut off Self. Clipstobounds=YES; }return Self;} - (void) Setphoto: (Hwphoto *) photo{_photo = photo;//Set Picture[ Selfsd_setimagewithurl:[NsurlUrlwithstring:photo. Thumbnail_pic] Placeholderimage:[UIImageimagenamed:@"Timeline_image_placeholder"]];//show \ Hide gif controls //judgment is enough to end with GIF or GIF Self. Gifview. Hidden= ! [Photo. Thumbnail_pic. lowercasestringhassuffix:@"GIF"];} - (void) layoutsubviews{[SuperLayoutsubviews]; Self. Gifview. x= Self. Width- Self. Gifview. Width; Self. Gifview. Y= Self. Height- Self. Gifview. Height;}@end
Three: note the local display \ Hide gif control
// 判断是够以gif或者GIF结尾 self.gifView.hidden = ![photo.thumbnail_pic.lowercaseString hasSuffix:@"gif"];
String classification is based on the string font and the maximum width to get the height of the width occupied
/** * 根据字符串字体和最大宽度来得到所占据的高度宽度 * * @param font 字体 * @param maxW 最大宽度 * * @return 长宽size */- (CGSize)sizeWithFont:(UIFont *)font maxW:(CGFloat)maxW{ NSMutableDictionary *attrs = [NSMutableDictionary dictionary]; attrs[NSFontAttributeName] = font; CGSize maxSize = CGSizeMake(maxW, MAXFLOAT); return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size;}/** * 在宽度为最大值时候根据字体得到宽高 * * @param font 字体 * * @return 长宽size */- (CGSize)sizeWithFont:(UIFont *)font{ return [self sizeWithFont:font maxW:MAXFLOAT];}
Uiimageview Picture Settings
/** Uiviewcontentmodescaletofill: The picture stretches to fill the entire uiimageview (picture May deform) Uiviewcontentmodescaleaspectfit: Picture Pull Extend to full display in Uiimageview (picture will not deform) Uiviewcontentmodescaleaspectfill: The picture is stretched to the width of the image equal to the width of the uiimageview or the height of the picture is equal to u Iimageview height Uiviewcontentmoderedraw: When the Setneedsdisplay method is called, the picture is re-rendered Uiviewcontentmodecenter: Center display Uiviewcontentmodetop, Uiviewcontentmodebottom, Uiviewcontentmodeleft, Uiviewcontentmoderig HT, Uiviewcontentmodetopleft, Uiviewcontentmodetopright, Uiviewcontentmodebottomleft, UIVi Ewcontentmodebottomright, rule of experience: 1. Any picture with a scale word will stretch 2. Any picture with aspect words will maintain the original aspect ratio, the picture will not deform */ //Content mode self (ImageView object)Self.contentmode = Uiviewcontentmodescaleaspectfill;//out-of-border content is cut offSelf.clipstobounds = YES;
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Cat Learning iOS Weibo project Combat (10) Introduction to the display of images in Weibo cell and various fill modes