您的位置 首页 5G

iphone开发中的一些小技巧

1、如果在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:UIImage *image = [UIImage imageNamed:filename];CGImageRef im

1、假如在程序中想对某张图片进行处理的话(得到某张图片的一部分)可一用以下代码:

UIImage *image = [UIImage imageNamed:filename];

CGImageRef imageRef = image.CGImage;

CGRect rect = CGRectMake(origin.x, origin.y ,size.width, size.height);

CGImageRef imageRefRect = CGImageCreateWithImageInRect(imageRef, rect);

UIImage *imageRect = [[UIImage alloc] initWithCGImage:imageRefRect];

2、判别设备是iphone仍是iphone4的代码:

#define isRetina ([UIScreen instancesRespondToSelector:@selector(currentMode)] ? CGSizeEqualToSize(CGSizeMake(640, 960), [[UIScreen mainScreen] currentMode].size) : NO)

3、判别邮箱输入的是否正确:

– (BOOL) validateEmail: (NSString *) candidate {

NSString *emailRegex = @[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4};

NSPredicate *emailTest = [NSPredicate predicateWithFormat:@SELF MATCHES %@, emailRegex];

return [emailTest evaluateWithObject:candidate];

}

4、怎么把当时的视图作为相片保存到相册中去:

#import

UIGraphicsBeginImageContext(currentView.bounds.size); //currentView 当时的view

[currentView.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

5、本地告诉(类似于push告诉)按home键到后台 十秒后触发:

UILocalNotification *notification=[[UILocalNotification alloc] init];

if (notification!=nil) {

NSLog(@>> support local notification);

NSDate *now=[NSDate new];

notification.fireDate=[now addTimeInterval:10];

notification.timeZone=[NSTimeZone defaultTimeZone];

notification.alertBody=@该去吃晚饭了!;

[[UIApplication sharedApplication].scheduleLocalNotification:notification];

}

6、捕获iphone通话事情:

CTCallCenter *center = [[CTCallCenter alloc] init];

center.callEventHandler = ^(CTCall *call)

{

NSLog(@call:%@, call.callState);

}

7、iOS 4 引入了多任务支撑,所以用户按下 “Home” 键今后程序或许并没有退出而是转入了后台运转。假如您想让使用直接退出,最简略的办法是:在 info-plist 里边找到 Application does not run in background 一项,勾选即可。

8、使UIimageView的图画旋转:

float rotateAngle = M_PI;

CGAffineTransform transform =CGAffineTransformMakeRotation(rotateAngle);

imageView.transform = transform;

9、设置旋转的原点:

#import

UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@bg.png]];

imageView.layer.anchorPoint = CGPointMake(0.5, 1.0);

10、完成自界说的状态栏(隐瞒状态栏):

CGRect frame = {{0, 0}, {320, 20}};

UIWindow* wd = [[UIWindow alloc] initWithFrame:frame];

[wd setBackgroundColor:[UIColor clearColor]];

[wd setWindowLevel:UIWindowLevelStatusBar];

frame = CGRectMake(100, 0, 30, 20);

UIImageView* img = [[UIImageView alloc] initWithFrame:frame];

[img setContentMode:UIViewContentModeCenter];

[img setImage:[UIImage imageNamed:@00_0103.png]];

[wd addSubview:img];

[wd makeKeyAndVisible];

[UIView beginAnimations:nil context:nil];

[UIView setAnimationDuration:2];

frame.origin.x += 150;

[img setFrame:frame];

[UIView commitAnimations];

11、在程序中完成电话的拨打:

//增加电话图标按钮

UIButton *btnPhone = [[UIButton buttonWithType:UIButtonTypeCustom] retain];

btnPhone.frame = CGRectMake(280,10,30,30);

UIImage *image = [UIImage imageNamed:@phone.png];

[btnPhone setBackgroundImage:image forState:UIControlStateNormal];

//点击拨号按钮直接拨号

[btnPhone addTarget:self action:@selector(callAction:event:)forControlEvents:UIControlEventTouchUpInside];

[cell.contentView addSubview:btnPhone]; //cell是一个UITableViewCell

//界说点击拨号按钮时的操作

– (void)callAction:(id)sender event:(id)event{

NSSet *touches = [event allTouches];

UITouch *touch = [touches anyObject];

CGPoint currentTouchPosition = [touch locationInView:self.listTable];

NSIndexPath *indexPath = [self.listTable indexPathForRowAtPoint: currentTouchPosition];

if (indexPath == nil) {

return;

}

NSInteger section = [indexPath section];

NSUInteger row = [indexPath row];

NSDictionary *rowData = [datas objectAtIndex:row];

NSString *num = [[NSString alloc] initWithFormat:@tel://%@,number]; //number为号码字符串

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:num]]; //拨号

}

12、更改iphone的键盘色彩:

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/yingyong/5g/297574.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部