Practice of Tmall's iOS Weibo Project (10) Image Display and various filling modes in Weibo cell, ioscell
CAT/CAT sharing, must be excellent
For Original Articles, please reprint them. Reprinted Please note: Yan Nai-yu's blog
Address: http://blog.csdn.net/u013357243
: 1. Effect
If you directly set the image to be stretched, we will introduce some details about the image display.
Ii. Code
Code implementation is actually very simple. Weibo uses a photos to store these images, and then uses a photo class for each photo, and displays gif and other styles on it. There are many tips, directly Add code
It is not difficult for JIU Gong GE to set columns or other algorithms.
# Import "HWStatusPhotosView. h "# import" HWPhoto. h "# import" HWStatusPhotoView. h "# define HWStatusPhotoWH 70 # define HWStatusPhotoMargin 10 # define HWStatusPhotoMaxCol (count) (count = 4 )? 2: 3) @ implementation HWStatusPhotosView // 9-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {} return self ;} -(void) setPhotos :( NSArray *) photos {_ photos = photos; int photosCount = photos. count; // create a sufficient number of image controls // self. subviews. do not assign values to other variables while (self. subviews. count <photosCount) {HWStatusPhotoView * photoView = [[HWStatusPhotoView alloc] init]; [self addSub View: photoView] ;}// traverse all image controls and set the image for (int I = 0; I <self. subviews. count; I ++) {HWStatusPhotoView * photoView = self. subviews [I]; if (I <photosCount) {// display photoView. photo = photos [I]; photoView. hidden = NO;} else {// hide photoView. hidden = YES ;}}- (void) layoutSubviews {[super layoutSubviews]; // sets the image size and position int photosCount = self. photos. count; int maxCol = HWStatusPhotoMaxCol (photosCount); For (int I = 0; I <photosCount; I ++) {HWStatusPhotoView * photoView = self. subviews [I]; int col = I % maxCol; photoView. x = col * (HWStatusPhotoWH + HWStatusPhotoMargin); int row = I/maxCol; photoView. y = row * (HWStatusPhotoWH + HWStatusPhotoMargin); photoView. width = HWStatusPhotoWH; photoView. height = HWStatusPhotoWH ;}+ (CGSize) sizeWithCount :( int) count {// maximum number of columns (maximum number of columns in a row) int maxCols = HWSt AtusPhotoMaxCol (count); int cols = (count> = maxCols )? MaxCols: count; CGFloat photosW = cols * HWStatusPhotoWH + (cols-1) * HWStatusPhotoMargin; // number of rows int rows = (count + maxCols-1)/maxCols; CGFloat photosH = rows * HWStatusPhotoWH + (rows-1) * HWStatusPhotoMargin; return CGSizeMake (photosW, photosH);} @ end
Photo 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 = [UIImage imageNamed: @ "timeline_image_gif"]; UIImageView * gifView = [[UIImageView alloc] initWithImage: image]; [self addSubview: gifView]; self.gif View = gifView;} return _ gifView;}-(id) initWithFrame :( CGRect) frame {self = [super initWithFrame: frame]; if (self) {// content mode self. contentMode = UIViewContentModeScaleAspectFill; // content beyond the border is cut off self. clipsToBounds = YES;} return Self;}-(void) setPhoto :( HWPhoto *) photo {_ photo = photo; // set the image [self sd_setImageWithURL: [NSURL URLWithString: photo. thumbnail_pic] placeholderImage: [UIImage imageNamed: @ "timeline_image_placeholder"]; // display \ hide gif control // determine whether self.gif View ends with gif or GIF. hidden =! [Photo. thumbnail_pic.lowercaseString hasSuffix: @ "gif"];}-(void) layoutSubviews {[super layoutSubviews]; self.gif View. x = self. width-self.gif View. width; self.gif View. y = self. height-self.gif View. height;} @ end
3. Note that the local display \ hide gif Control
// The result is that self.gif View. hidden = is ended with a gif or GIF! [Photo. thumbnail_pic.lowercaseString hasSuffix: @ "gif"];
String classification obtains the height and width of a string based on its font and maximum width.
/*** Obtain the height and width occupied by the string font and the maximum width ** @ param font * @ param maxW maximum width ** @ return long width size */-(CGSize) sizeWithFont :( UIFont *) font maxW :( CGFloat) maxW {NSMutableDictionary * attrs = [Symbol dictionary]; attrs [character] = font; CGSize maxSize = CGSizeMake (maxW, MAXFLOAT ); return [self boundingRectWithSize: maxSize options: NSStringDrawingUsesLineFragmentOrigin attributes: attrs context: nil]. size;}/*** get width and height based on the font when the width is the maximum ** @ param font ** @ return length and width size */-(CGSize) sizeWithFont :( UIFont *) font {return [self sizeWithFont: font maxW: MAXFLOAT];}
UIImageView image settings
/** UIViewContentModeScaleToFill: stretch the image to fill the entire UIImageView (the image may be deformed) UIViewContentModeScaleAspectFit: stretch the image until it is fully displayed in UIImageView (the image will not be deformed) uiviewcontentmodescaleas: when the setNeedsDisplay method is called, the image is re-rendered and the UIViewContentModeTop is displayed in the center, UIViewContentModeBottom, UIViewContentModeLeft, UIViewContentModeRight, UIViewContentModeTopLeft, UIViewContentModeTopRight, UIViewContentModeBottomLeft, UIViewContentModeBottomRight, rule of experience: 1. any image with the word Scale will be stretched 2. all words with Aspect will keep the original Aspect ratio, and the image will not be deformed * // content mode self (imageView object) self. contentMode = UIViewContentModeScaleAspectFill; // content beyond the border is cut off self. clipsToBounds = YES;
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.