1. 程式人生 > >iOS開發之-收集的不常用卻實用的小方法和技巧

iOS開發之-收集的不常用卻實用的小方法和技巧

1.顏色轉變成圖片

- (UIImage *)createImageWithColor:(UIColor *)color
{
    CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);
    UIImage
*theImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return theImage; }

2.app評分跳轉

-(void)goToAppStore
{
    NSString *str = [NSString stringWithFormat:
                     @"itms-apps://ax.itunes.apple.com/WebObjects/MZStore.woa/wa/viewContentsUserReviews?type=Purple+Software&id=%d"
,547203890]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]]; }

3.獲取當前系統語言環境

NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSArray *languages = [defs objectForKey:@"AppleLanguages"];
NSString *preferredLang = [languages objectAtIndex:0];

4.計算字串的高度

NSString *str = @"chuanzhang"
; NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init]; paragraphStyle.lineBreakMode = NSLineBreakByWordWrapping; NSDictionary *dicAtt = [NSDictionary dictionaryWithObjectsAndKeys:[UIFont systemFontOfSize:15],NSFontAttributeName,paragraphStyle.copy,NSParagraphStyleAttributeName, nil]; NSAttributedString *attribute = [[NSAttributedString alloc]initWithString:str attributes:dicAtt]; CGRect frame = [attribute boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil];

5.強行關閉app的方法

// 私有API
[[UIApplication sharedApplication] performSelector:@selector(terminateWithSuccess)];
// C語言方法
exit(0);

6.如何快速的檢視一段程式碼的執行時間

#define TICK   NSDate *startTime = [NSDate date]
#define TOCK   NSLog(@"Time: %f", -[startTime timeIntervalSinceNow])
// 在想要檢視執行時間的程式碼的地方進行這麼處理
TICK
//do your work here
TOCK

7.在使用view的縮放的時候,layer.border.width隨著view的放大,會出現鋸齒化的問題,解決這個問題需要設定這個屬性。

self.layer.allowsEdgeAntialiasing = YES;

8.instrument中time profile 中的self, #self,%self各代表什麼 ?


333.jpeg

下面引用了一下網上的具體內容

Self is "The number of times the symbol calls itself." according to the Apple Docs on the Time Profiler.

From the way the numbers look though, it seems self is the summed duration of samples that had this symbol at the bottom of its stack trace. That would make:

self: the number of samples where this symbol was at the bottom of the stack trace

% self: the percent of self samples relative to total samples of currently displayed call tree

(eg - #self / total samples).
So this wouldn't tell you how many times a method was called. But it would give you an idea how much time is spent in a method or lower in the call tree.

9.神器計算圖片位置的函式:AVMakeRectWithAspectRatioInsideRect()
通過這個函式,我們可以計算一個圖片放在另一個 view 按照一定的比例居中顯示,可能說的我比較抽象,還是用圖來顯示,可以說它可以直接一個 image 以任何的比例顯示顯示在 imageview 中居中所處的位置,拿 UIViewContontAspectFit來演示

UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];
imageView.center = self.view.center;
imageView.backgroundColor = [UIColor redColor];
imageView.contentMode = UIViewContentModeScaleAspectFit;
UIImage *image = [UIImage imageNamed:@"mm.jpg"];
imageView.image = image;

 CGRect iamgeAspectRect = AVMakeRectWithAspectRatioInsideRect(image.size, imageView.bounds);
NSLog(@"iamgeAspectRect = %@, imageView =%@",NSStringFromCGRect(iamgeAspectRect),NSStringFromCGRect(imageView.frame));
[self.view addSubview:imageView];

圖片顯示如下:


mm.png

log 打因結果如下:

iamgeAspectRect = {{37.563884156729145, 0}, {224.87223168654171, 300}}, imageView ={{37.5, 183.5}, {300, 300}}

可以從 log 得出 對應的 image 以 aspectFit 的方式在 imageView 的位置,在 imageView 中的位置是(37.5,0)。這樣你根本不需要任何多的程式碼來計算了。(ps:這個函式是在 AV框架的,童鞋們自行匯入。)

具體它的其他的好處,如果你是做相機或者圖片處理的你就知道它的好處了,什麼處理橫屏照片了,16:9,1:1,4:3圖片在控制元件中的位置,控制元件上的點對應圖片上的點的位置拉,等等。

10.關於 如果一個矩形如果做了平移旋轉縮放等一系列操作之後,上下左右的四個點(甚至矩形上任意一個點)的位置。

CGPoint originalCenter = CGPointApplyAffineTransform(_mStyleLeftEyeView.center,
                                                     CGAffineTransformInvert(_mStyleLeftEyeView.transform));

//1左眼內眼角
CGPoint bottomRight = originalCenter;
bottomRight.x += _mStyleLeftEyeView.bounds.size.width / 2;
bottomRight.y += _mStyleLeftEyeView.bounds.size.height / 2;
bottomRight = CGPointApplyAffineTransform(bottomRight, _mStyleLeftEyeView.transform);

首先這個 styleLeftView 就是一個矩形的 view,這裡以右下角的點做示範,做無論做了任何的 tranform 之後都可以得到它的點的位置
11.tableViewCell上的button,點選獲取所在row

UITableViewCell *cell = (UITableViewCell *)[[btn superview] superview];
NSIndexPath * indexPath = [self.tableView indexPathForCell:cell];

12.設定貼上內容

[UIPasteboard generalPasteboard].string = @"string";
// 獲取貼上內容
 NSString *string = [UIPasteboard generalPasteboard].string;

13.iPhone為了節省電力所以有一個自動休眠機制,如果想讓我們的APP不自動進入休眠只需要設定 UIApplication的idleTimerDisabled 屬性為 YES 即可。(切勿濫用)
示例:

[UIApplication sharedApplication].idleTimerDisabled = YES;

14.UIApplicationUserDidTakeScreenshotNotification通知,當用戶截圖時觸發

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(screenCapture) name:UIApplicationUserDidTakeScreenshotNotification object:nil];
- (void)screenCapture{
    // doSomething
}


文/船長_(簡書作者)
原文連結:http://www.jianshu.com/p/a492eb6ad7fa
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。