小編給大家分享一下iOS如何獲取屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!

創(chuàng)新互聯(lián)建站主要從事網(wǎng)站建設(shè)、做網(wǎng)站、網(wǎng)頁設(shè)計(jì)、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)蘇尼特右,十多年網(wǎng)站建設(shè)經(jīng)驗(yàn),價(jià)格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):13518219792
摘要:獲得屏幕圖像,label的動(dòng)態(tài)size,時(shí)間戳轉(zhuǎn)化為時(shí)間,RGB轉(zhuǎn)化成顏色,加邊框,壓縮圖片,textfield的placeholder,圖片做灰度處理
1.獲得屏幕圖像
- (UIImage *)imageFromView: (UIView *) theView
{
UIGraphicsBeginImageContext(theView.frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
[theView.layer renderInContext:context];
UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return theImage;
}2.label的動(dòng)態(tài)size
- (CGSize)labelAutoCalculateRectWith:(NSString*)text FontSize:(CGFloat)fontSize MaxSize:(CGSize)maxSize
{
NSMutableParagraphStyle* paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineBreakMode=NSLineBreakByWordWrapping;
NSDictionary* attributes =@{NSFontAttributeName:[UIFont fontWithName:@"MicrosoftYaHei" size:fontSize],NSParagraphStyleAttributeName:paragraphStyle.copy};
CGSize labelSize = [text boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:attributes context:nil].size;
labelSize.height=ceil(labelSize.height);
return labelSize;
}3.時(shí)間戳轉(zhuǎn)化為時(shí)間
-(NSString*)TimeTrasformWithDate:(NSString *)dateString
{
NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YY-MM-dd HH:mm"];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Beijing"]];
NSString *date = [formatter stringFromDate:[NSDate dateWithTimeIntervalSince1970:dateString.integerValue]];
//NSLog(@"date1:%@",date);
return date;
}4.RGB轉(zhuǎn)化成顏色
+ (UIColor *)colorFromHexRGB:(NSString *)inColorString
{
UIColor *result = nil;
unsigned int colorCode = 0;
unsigned char redByte, greenByte, blueByte;
if (nil != inColorString)
{
NSScanner *scanner = [NSScanner scannerWithString:inColorString];
(void) [scanner scanHexInt:&colorCode]; // ignore error
}
redByte = (unsigned char) (colorCode >> 16);
greenByte = (unsigned char) (colorCode >> 8);
blueByte = (unsigned char) (colorCode); // masks off high bits
result = [UIColor
colorWithRed: (float)redByte / 0xff
green: (float)greenByte/ 0xff
blue: (float)blueByte / 0xff
alpha:1.0];
return result;
}5.加邊框
UIRectCorner corners=UIRectCornerTopLeft | UIRectCornerTopRight; UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:corners cornerRadii:CGSizeMake(4, 0)]; CAShapeLayer *maskLayer = [CAShapeLayer layer]; maskLayer.frame = view.bounds; maskLayer.path = maskPath.CGPath; view.layer.mask = maskLayer;
6.//壓縮圖片
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
//創(chuàng)建一個(gè)圖形上下文形象
UIGraphicsBeginImageContext(newSize);
// 告訴舊圖片畫在這個(gè)新的環(huán)境,所需的
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
//獲取上下文的新形象
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// 結(jié)束上下文
UIGraphicsEndImageContext();
return newImage;
}7.textfield的placeholder
[textF setValue:[UIColor whiteColor] forKeyPath:@"_placeholderLabel.textColor"]; [textF setValue:[UIFont boldSystemFontOfSize:15] forKeyPath:@"_placeholderLabel.font"];
8.布局
butLeft. imageEdgeInsets = UIEdgeInsetsMake (7 , 5 , 7 , 25 ); butLeft.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
9.//調(diào)用此方法改變label最后2個(gè)字符的大小
- (void)label:(UILabel *)label BehindTextSize:(NSInteger)integer
{
NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc] initWithString:label.text];
[mutaString addAttribute:NSFontAttributeName value:[UIFont boldSystemFontOfSize:16] range:NSMakeRange(label.text.length-2, 2)];
label.attributedText = mutaString;
}10.
- (void)ChangeLabelTextColor:(UILabel *)label
{
NSMutableAttributedString *mutaString = [[NSMutableAttributedString alloc] initWithString:label.text];
[mutaString addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:207/255.0 green:34/255.0 blue:42/255.0 alpha:1] range:NSMakeRange(0, 5)];
label.attributedText = mutaString;
}if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[tableView setLayoutMargins:UIEdgeInsetsZero];
}
}
// Do any additional setup after loading the view.
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([[UIDevice currentDevice].systemVersion floatValue] >= 8.0) {
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
}11.圖片變灰度
-(UIImage *) grayscaleImage: (UIImage *) image
{
CGSize size = image.size;
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width,
image.size.height);
// Create a mono/gray color space
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
CGContextRef context = CGBitmapContextCreate(nil, size.width,
size.height, 8, 0, colorSpace, kCGImageAlphaNone);
CGColorSpaceRelease(colorSpace);
// Draw the image into the grayscale context
CGContextDrawImage(context, rect, [image CGImage]);
CGImageRef grayscale = CGBitmapContextCreateImage(context);
CGContextRelease(context);
// Recover the image
UIImage *img = [UIImage imageWithCGImage:grayscale];
CFRelease(grayscale);
return img;
}13.16進(jìn)制轉(zhuǎn)rgb
#define UIColorFromRGB(rgbValue) [UIColor colorWithRed:((float)((rgbValue & 0xFF0000) >> 16))/255.0 green:((float)((rgbValue & 0xFF00) >> 8))/255.0 blue:((float)(rgbValue & 0xFF))/255.0 alpha:1.0]
看完了這篇文章,相信你對“iOS如何獲取屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size”有了一定的了解,如果想了解更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝各位的閱讀!
新聞名稱:iOS如何獲取屏幕圖像、壓縮圖片、加邊框、調(diào)整label的size
URL分享:http://www.chinadenli.net/article6/gshgig.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站策劃、網(wǎng)站內(nèi)鏈、電子商務(wù)、品牌網(wǎng)站設(shè)計(jì)、微信小程序、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)