打印所有視圖的子視圖
po [[self view] recursiveDescription]
NSString判斷特殊字符
if ([string rangeOfCharacterFromSet:[NSCharacterSet decimalDigitCharacterSet].invertedSet].location != NSNotFound) {
NSLog(@"%@", @"This field accepts only numeric entries.");
}
NSCharacterSet *set = [[NSCharacterSet characterSetWithCharactersInString:@"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet];
if ([string rangeOfCharacterFromSet:set].location != NSNotFound) {
NSLog(@"This string contains illegal characters");
}
去除tableView中分割線多余的15個像素
- 首先在
viewDidLoad
方法加入以下代碼:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}
- 然后在代理方法
willDisplayCell
中加入以下代碼
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell
forRowAtIndexPath:(NSIndexPath *)indexPath {
if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
[cell setSeparatorInset:UIEdgeInsetsZero];
}
if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
[cell setLayoutMargins:UIEdgeInsetsZero];
}
}
iOS直接退出應用
AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
UIWindow *window = app.window;
[UIView animateWithDuration:1.0f animations:^{
window.alpha = 0;
}complain:^(BOOL finished){
exit(0);
}];
數組快速求和、最大值、最小值、平均值
NSArray *array = @[@"1.0",@"2.1",@"3.2",@"4.3"] ;
NSNumber *sum = [array valueForKeyPath:@"sum.floatValue"] ;
NSNumber *avg = [array valueForKeyPath:@"avg.floatValue"] ;
NSNumber *max = [array valueForKeyPath:@"max.floatValue"] ;
NSNumber *min = [array valueForKeyPath:@"min.floatValue"] ;
修改Label中不同文字的顏色
- (void)editStringColor:(NSString *)string editStr:(NSString *)editStr color:(UIColor *)color {
// string為整體字符串, editStr為需要修改的字符串
NSRange range = [string rangeOfString:editStr];
NSMutableAttributedString *attribute = [[NSMutableAttributedString alloc] initWithString:string];
// 設置屬性修改字體顏色UIColor或大小UIFont
[attribute addAttributes:@{NSForegroundColorAttributeName:color} range:range];
self.label.attributedText = attribute;
}
播放聲音
//1.獲取音效資源的路徑
NSString *path = [[NSBundle mainBundle]pathForResource:@"sound" ofType:@"wav"];
//2.將路徑轉化為url
NSURL *tempUrl = [NSURL fileURLWithPath:path];
//3.用轉化成的url創建一個播放器
NSError *error = nil;
AVAudioPlayer *play = [[AVAudioPlayer alloc]initWithContentsOfURL:tempUrl error:&error];
//4.播放
[play play];
播放音效
SystemSoundID soundId ;
NSURL *soundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:urlStr ? urlStr : @"" ofType:nil]] ;
AudioServicesCreateSystemSoundID((__bridge CFURLRef _Nonnull)soundURL, &soundId) ;
if(didShock) {
//播放音效時帶振動
AudioServicesPlayAlertSound(soundId) ;
}
else {
//播放音效時不帶振動
AudioServicesPlaySystemSound(soundId) ;
}
修改TabBar Item
的屬性
- 修改標題位置
self.tabBarItem.titlePositionAdjustment = UIOffsetMake(0, -10);
- 修改圖片位置
self.tabBarItem.imageInsets = UIEdgeInsetsMake(-3, 0, 3, 0);
- 批量修改屬性
for (UIBarItem *item in self.tabBarController.tabBar.items) {
[item setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIFont fontWithName:@"Helvetica" size:19.0], NSFontAttributeName, nil] forState:UIControlStateNormal];
}
- 未選中字體顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor greenColor]} forState:UIControlStateNormal];
- 選中字體顏色
[[UITabBarItem appearance] setTitleTextAttributes:@{NSForegroundColorAttributeName:[UIColor cyanColor]} forState:UIControlStateSelected];
修改UItextField中placeholder的文字顏色和字體大小
[textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];
[textField setValue:[UIFont boldSystemFontOfSize:16] forKeyPath:@“_placeholderLabel.font”];
判斷對像是否遵循了某協議
if ([self.selectedController conformsToProtocol:@protocol(RefreshPtotocol)]) {
[self.selectedController performSelector:@selector(onTriggerRefresh)];
}
側滑手勢
- 禁止側滑手勢
- (void)closePopGestureRecognizer{
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = NO;
self.navigationController.interactivePopGestureRecognizer.delegate = self;
}
}
- 開啟側滑手勢
- (void)openPopGestureRecognizer{
if ([self.navigationController respondsToSelector:@selector(interactivePopGestureRecognizer)]) {
self.navigationController.interactivePopGestureRecognizer.enabled = YES;
self.navigationController.interactivePopGestureRecognizer.delegate = nil;
}
}
修改Label行間距
NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.contentLabel.text];
NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
[paragraphStyle setLineSpacing:3];
//調整行間距
[attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.contentLabel.text length])];
self.contentLabel.attributedText = attributedString;
字符串相關操作
- 去除所有的空格
[str stringByReplacingOccurrencesOfString:@" " withString:@""];
- 去除首尾的空格
[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
- 全部字符轉為大寫字母
- (NSString *)uppercaseString;
- 全部字符轉為小寫字母
- (NSString *)lowercaseString;
設置滑動的時候隱藏導航欄
self.navigationController.hidesBarsOnSwipe = YES ;
mac下wav轉caf格式
打開終端(Terminal),先進入wav文件所在的目錄,輸入命令:/usr/bin/afconvert -f caff -d LEI16 “name.wav”
scrollView的觸摸事件傳遞
從你的手指touch屏幕開始,scrollView開始一個timer,如果:
- 150ms內如果你的手指沒有任何動作,消息就會傳給subView
- 150ms內手指有明顯的滑動(一個swipe動作),scrollView就會滾動,消息不會傳給subView
- 150ms內手指沒有滑動,scrollView將消息傳給subView,但是之后手指開始滑動,scrollView傳送touchesCancelled消息給subView,然后開始滾動。
觀察下tableView的情況,你先按住一個cell,cell開始高亮,手不要放開,開始滑動,tableView開始滾動,高亮取消。
delaysContentTouches的作用:這個標志默認是YES,使用上面的150ms的timer,如果設置為NO,touch事件立即傳遞給subView,不會有150ms的等待。
cancelsTouches的作用:這個標準默認為YES,如果設置為NO,這消息一旦傳遞給subView,這scroll事件不會再發生。
mac下顯示編譯時間
打開終端,輸入命令defaults write com.apple.dt.Xcode ShowBuildOperationDuration YES
UIImageView序列幀動畫
UIImageView *tom = [UIImageView alloc]init] ;
NSMutableArray *arrayM=[NSMutableArray array];
for (int i=0; i<40; i++) {
[arrayM addObject:[UIImage imageNamed:[NSString stringWithFormat:@"eat_%02d.jpg",i]]];
}
//0.是否正在動畫
[self.tom isAnimating];
//1.設置圖片的數組
[self.tom setAnimationImages:arrayM];
//2.設置動畫播放次數
[self.tom setAnimationRepeatCount:1];
//3.設置動畫時長,默認每秒播放30張圖片
[self.tom setAnimationDuration:40*0.075];
//4.開始動畫
[self.tom startAnimating];
//5.動畫播放完成后,清空動畫數組
[self.tom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.tom.animationDuration];
補充
Images.xcassets中的素材只支持png格式的圖片
圖片只支持[UIImage imageNamed]的方式實例化,但是不能從Bundle中加載
在編譯時,Images.xcassets中的所有文件會被打包為Assets.car的文件
跳轉至應用系統權限配置
NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString] ;
if([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url] ;
}
判斷某個viewController是否正在顯示
- (BOOL)isVisable {
return (_curViewController.isViewLoaded && _curViewController.view.window) ;
}
- (UIViewController*)getCurrentVC {
UIViewController *displayVC = nil;
UIWindow *window = [[UIApplication sharedApplication] keyWindow] ;
if(window.windowLevel != UIWindowLevelNormal)
{
NSArray *windows = [[UIApplication sharedApplication] windows] ;
for(UIWindow *tmpWindow in windows) {
if(tmpWindow.windowLevel == UIWindowLevelNormal) {
window = tmpWindow ;
break;
}
}
}
id nextResponder = nil ;
UIViewController *rootVC = window.rootViewController ;
if(rootVC.presentedViewController) {
nextResponder = rootVC.presentedViewController;
}
else {
UIView *frontView = [[window subviews] objectAtIndex:0] ;
nextResponder = [frontView nextResponder] ;
}
if([nextResponder isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabVC = (UITabBarController*)nextResponder ;
UINavigationController *navVC = tabVC.selectedViewController ;
displayVC = navVC.childViewControllers.lastObject;
}
else if([nextResponder isKindOfClass:[UINavigationController class]]) {
UINavigationController *navVC = (UINavigationController*)nextResponder ;
displayVC = navVC.childViewControllers.lastObject;
}
else {
displayVC = nextResponder ;
}
return displayVC ;
}
檢查第三方APP是否安裝及跳轉啟動
通過[[UIApplication sharedApplication] canOpenUrl:[NSURL URLWithString:@""]]
判斷是否安裝第三方app
通過[[UIApplication sharedApplication] openUrl:[NSURL URLWithString:@""]]
跳轉啟動
控件局部圓角
CGRect rect = CGRectMake(0, 0, button.bounds.size.width, button.bounds.size.height) ;
CGSize radio = CGSizeMake(5, 5); //圓角尺寸
UIRectCorner corner = UIRectCornerTopLeft|UIRectCornerTopRight;//圓角位置
UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corner cornerRadii:radio];
//圖層蒙版
CAShapeLayer *masklayer = [[CAShapeLayer alloc]init];
masklayer.frame = button.bounds;
masklayer.path = path.CGPath;
button.layer.mask = masklayer;
隱藏狀態欄
隱藏狀態欄有兩種途徑,一種是在應用層面,即整個應用隱藏狀態欄;另一種是在控制器層面,即在該控制器隱藏狀態欄。使用哪種途徑由info.plist
文件中的View controller-based status bar appearence
項來決定的。
-
View controller-based status bar appearence
項設為YES,則viewController對狀態欄的設置優先級高于application
對狀態欄的設置,具體的代碼為:
- 在
viewWillAppear
或viewDidAppear
中添加代碼:
if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)]) {
[self prefersStatusBarHidden];
[self performSelector:@selector(setNeedsStatusBarAppearanceUpdate)];
}
- 覆蓋
viewController
的prefersStatusBarHidden
方法的實現,返回YES:
- (BOOL)prefersStatusBarHidden {
return YES;
}
-
View controller-based status bar appearence
項設為NO,則application
對狀態欄的設置優先級高于viewController
對狀態欄的設置,viewController
對狀態欄的設置無效,具體的代碼為:
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO] ;
- 建議使用第一種方法。因為第一種方法之影響當前viewController,而第二種方法則影響整個應用。iOS9.0后第二種方法已經被棄用。
隱藏tableView底部多余的分割線
self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero] ;
tableView帶有動畫效果的刷新
通常刷新UITableView
采用的是[self.tableView reloadData]
,但是這種刷新方式沒有動畫效果,目前有兩種方式,一種是系統提供的,另一種是自定義的。
- 系統提供的方式
- (void)reloadSections:(NSIndexSet *)sections withRowAnimation:(UITableViewRowAnimation)animation ;
- (void)reloadRowsAtIndexPaths:(NSArray<NSIndexPath *> *)indexPaths withRowAnimation:(UITableViewRowAnimation)animation ;
對于方法1,如果UITableView
的樣式為UITableViewStylePlain
,則可以通過[NSIndexSet indexSetWithIndex:0]
生成相應的sections
;如果UITableView
的樣式為UITableViewStyleGrouped
,則可以通過[NSIndexSet indexSetWithIndex:section]生成相應的sections
。
- 自定義方式
在cell
中自定義動畫顯示方式,并且在UITableView
的代理方法- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
實現中適時調用。
NSString使用stringWithFormat的小技巧
- 保留2位小數
NSString *string = [NSString stringWithFormat:@"%.2f",M_PI] ;
- 用0補全
NSString *string = [NSString stringWithFormat:@"%02d",5] ;
- 包含特殊符號%
NSString *string = [NSString stringWithFormat:@"%d%%",5] ;
- 包含特殊符號"
NSString *string = [NSString stringWithFormat:@"%d%\"",5] ;
Button禁止觸摸事件的2種方式
- 會改變按鈕的狀態
button.enable = NO ;
- 不會改變按鈕的狀態
button.userInteractionEnabled = NO ;
忽略備份文件
+ (BOOL)addSkipBackupAttributeToItemAtPath:(NSString *) filePathString {
NSURL* fileURL= [NSURL fileURLWithPath: filePathString];
assert([[NSFileManager defaultManager] fileExistsAtPath: [fileURL path]]) ;
NSError *error = nil ;
BOOL success = [fileURL setResourceValue: [NSNumber numberWithBool: YES] forKey: NSURLIsExcludedFromBackupKey error: &error] ;
return success;
}
通過圖片Data數據第一個字節來獲取圖片擴展名
- (NSString *)contentTypeForImageData:(NSData *)data {
uint8_t c;
[data getBytes:&c length:1];
switch (c) {
case 0xFF:
return @"jpeg";
case 0x89:
return @"png";
case 0x47:
return @"gif";
case 0x49:
case 0x4D:
return @"tiff";
case 0x52:
if ([data length] < 12) {
return nil;
}
NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
return @"webp";
}
return nil;
}
return nil;
}
##在宏中的使用
在宏中,##
起連接的作用
#define weakSelf(type) __weak typeof(type) weak##type = type;
上述宏會把weak
和type
連接起來。
數組排序
- 簡單排序
聲明:
- (NSArray<objectType>*)sortedArrayUsingSelector:(SEL)comparator
使用:
//在自定義類中添加比較方法
- (NSComparisonResult)compare:(Person *)otherObject {
return [self.birthDate compare:otherObject.birthDate] ;
}
NSArray *sortedArray = [persons sortedArrayUsingSelector:@selector(compare:)] ;
- block語法
聲明:
- (NSArray<objectType>*)sortedArrayUsingComparator:(NSComparator)cmptr
- (void)sortUsingComparator:(NSComparator)cmptr
使用:
NSArray *sortedArray = [drinkDetails sortedArrayUsingComparator:^NSComparisonResult(id a, id b) {
NSDate *first = [(Person*)a birthDate] ;
NSDate *second = [(Person*)b birthDate] ;
return [first compare:second];
}] ;
- 高級排序
聲明:
-(NSArray<ObjectType>*)sortedArrayUsingDescriptors:(NSArray<NSSortDescriptor *> *)sortDescriptors
參數:
NSSortDescriptor
對象可以通過NSSortDescriptor
的+(instancetype)sortDescriptorWithKey:(nullable NSString *)key ascending:(BOOL)ascending
方法獲得。
使用:
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"birthDate" ascending:YES] ;
NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor] ;
NSArray *sortedArray = [drinkDetails sortedArrayUsingDescriptors:sortDescriptors] ;
- 相關常用方法
- (NSComparisonResult)compare:(NSString *)aString options:(NSStringCompareOptions)mask
typedef NS_OPTIONS (NSUInteger, NSStringCompareOptions)
{
NSCaseInsensitiveSearch,//不區分大小寫
NSLiteralSearch,//逐字節比較,區分大小寫(大寫在前)
NSNumericSearch,//含數字時整體比較數字
}
圖片拉伸
iOS 5.0之前
- (UIImage *)stretchableImageWithLeftCapWidth:(NSInteger)leftCapWidth topCapHeight:(NSInteger)topCapHeight;
- 功能:該方法是
UIImage
的一個實例方法,用來創建一個整體拉伸而指定區域不拉伸的UIImage
對象 - 參數:
-
leftCapWidth
:左邊不拉伸區域的寬度 -
topCapHeight
:上方不拉伸區域的高度
-
- 注意:
- 根據設置的寬度和高度,將接下來的一個像素進行左右擴展和上下拉伸
- 進行拉伸的是距離
UIImage
對象左邊沿leftCapWidth+1
處的一豎排像素和距離UIImage
對象上邊沿topCapHeight+1
處的一橫排像素 - 只是對一個像素進行復制拉伸,后面剩余的像素也不會拉伸
- 舉例:
UIImage *normalImg = [UIImage imageNamed:@"img_bg_n_red"] ;
UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[normalImg stretchableImageWithLeftCapWidth:floorf(normalImg.size.width * 0.5) topCapHeight:(normalImg.size.height * 0.5)]] ;
iOS 5.0
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets;
功能:該方法是
UIImage
的一個實例方法,用來創建一個整體拉伸而指定區域不拉伸的UIImage
對象-
參數:
capInsets
的top,left,bottom,right
分別指定上、左、下、右四個距離,具體如下圖所示:
注意:拉伸部分如下圖所示:
iOS 6.0
- (UIImage *)resizableImageWithCapInsets:(UIEdgeInsets)capInsets resizingMode:(UIImageResizingMode)resizingMode;
具體的與上一個方法的一致,只是多了一個參數resizingMode
來指定拉伸模式
模式 | 說明 |
---|---|
UIImageResizingModeStretch | 拉伸模式,通過拉伸來填充圖片 |
UIImageResizingModeTile | 平鋪模式,通過重復顯示來填充圖片 |
根據顏色和尺寸繪制圖片
+ (UIImage *)imageFromColor:(UIColor *)color size:(CGSize)size {
CGRect rect = CGRectMake(0.0f, 0.0f, size.width, size.height) ;
UIGraphicsBeginImageContext(rect.size) ;
CGContextRef context = UIGraphicsGetCurrentContext() ;
CGContextSetFillColorWithColor(context, [color CGColor]) ;
CGContextFillRect(context, rect) ;
UIImage *image = UIGraphicsGetImageFromCurrentImageContext() ;
UIGraphicsEndImageContext() ;
return image;
}
根據圖片和指定大小生成平鋪的圖片
- (UIImage *)spreadImage:(UIImage *)image WithSize:(CGSize)size {
UIView *tempView = [[UIView alloc] init] ;
tempView.bounds = (CGRect){CGPointZero, size} ;
tempView.backgroundColor = [UIColor colorWithPatternImage:image] ;
UIGraphicsBeginImageContext(size) ;
[tempView.layer renderInContext:UIGraphicsGetCurrentContext()] ;
UIImage *bgImage = UIGraphicsGetImageFromCurrentImageContext() ;
UIGraphicsEndImageContext() ;
return bgImage ;
}
UIButton標題文字居左
button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
button.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
裁剪圓形圖片
- 不帶邊框
+ (nullable UIImage *)clipCircleImageWithImage:(nullable UIImage *)image {
//1、開啟上下文
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0) ;
//2、設置裁剪區域
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:(CGRect){CGPointZero, image.size}] ;
[path addClip] ;
//3、繪制圖片
[image drawAtPoint:CGPointZero] ;
/*
//2、設置裁剪區域
CGContextRef ctx = UIGraphicsGetCurrentContext() ;
CGFloat radius = image.size.width/2.0 ;
CGContextAddArc(ctx, radius, radius, radius, 0, M_PI * 2, 0) ;
CGContextClip(ctx) ;
//3、繪制圖片
[image drawInRect:(CGRect){CGPointZero, image.size}] ;
*/
//4、獲取新圖片
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//5、關閉上下文
UIGraphicsEndImageContext();
//6、返回新圖片
return newImage;
}
- 帶邊框
+ (nullable UIImage *)clipCircleImageWithImage:(nullable UIImage *)image circleRect:(CGRect)rect borderWidth:(CGFloat)borderW borderColor:(nullable UIColor *)borderColor {
//1:開啟上下文
UIGraphicsBeginImageContext(image.size) ;
//2:設置邊框
UIBezierPath * path = [UIBezierPath bezierPathWithOvalInRect:rect] ;
[borderColor setFill] ;
[path fill] ;
//3:設置裁剪區域
UIBezierPath * clipPath = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(rect.origin.x + borderW , rect.origin.y + borderW , rect.size.width - borderW * 2, rect.size.height - borderW *2)];
[clipPath addClip];
//4:繪制圖片
[image drawAtPoint:CGPointZero];
//5:獲取新圖片
UIImage * newImage = UIGraphicsGetImageFromCurrentImageContext();
//6:關閉上下文
UIGraphicsEndImageContext();
//7:返回新圖片
return newImage;
}
tablviewCell中添加scrollView后點擊手勢沖突
scrollView.userInteractionEnabled = NO ;
[self.contentView addSubview:scrollView] ;
[self.contentView addGestureRecognizer:scrollView.panGestureRecognizer] ;
小數NSNumber轉換成NSString
NSNumber *num = @(8.30) ;
NSString *str = [NSString stringWithFormat:@"%lf", [num floatValue]];
NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:str];
NSString *str1 = [num1 stringValue];
設置圖片透明度
- (UIImage *)imageByApplyingAlpha:(CGFloat)alpha image:(UIImage*)image {
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0f);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGRect area = CGRectMake(0, 0, image.size.width, image.size.height);
CGContextScaleCTM(ctx, 1, -1);
CGContextTranslateCTM(ctx, 0, -area.size.height);
CGContextSetBlendMode(ctx, kCGBlendModeMultiply);
CGContextSetAlpha(ctx, alpha);
CGContextDrawImage(ctx, area, image.CGImage);
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}