码迷,mamicode.com
首页 > 移动开发 > 详细

IOS开发基础知识--碎片38

时间:2016-05-04 01:06:32      阅读:313      评论:0      收藏:0      [点我收藏+]

标签:

1:FCUUID获取设备标识的运用

a:作者 githun地址 https://github.com/fabiocaccamo/FCUUID

因为里面还用到作者的另外一个类UICKeyChainStore地址:https://github.com/kishikawakatsumi/UICKeyChainStore

b:在项目中添加 Security.framework

c:导入头文件 #import “FCUUID.h"

// 每次运行应用都会变

+(NSString *)uuid;

//changes each time (no persistent), but allows to keep in memory more temporary uuids

+(NSString *)uuidForKey:(id<NSCopying>)key;

// 每次运行应用都会变

+(NSString *)uuidForSession;

// 重新安装的时候会变

+(NSString *)uuidForInstallation;

// 卸载后重装会变

+(NSString *)uuidForVendor;

// 抹掉iPhone的时候才会变,适合做唯一标识

+(NSString *)uuidForDevice;

技术分享

 

2:在图片增加一个外围的白色边框

-(UIImage*) circleImage:(UIImage*) image withParam:(CGFloat) inset {
    
    //UIGraphicsBeginImageContext(image.size);
    //解决失真 模糊的问题
    UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);
    CGContextRef context =UIGraphicsGetCurrentContext();
    
    //圆的边框宽度为2,颜色为红色
    
    CGContextSetLineWidth(context,2);
    
    CGContextSetStrokeColorWithColor(context, [UIColor whiteColor].CGColor);
    
    CGRect rect = CGRectMake(inset, inset, image.size.width - inset *2.0f, image.size.height - inset *2.0f);
    
    CGContextAddEllipseInRect(context, rect);
    
    CGContextClip(context);
    
    //在圆区域内画出image原图
    
    [image drawInRect:rect];
    
    CGContextAddEllipseInRect(context, rect);
    
    CGContextStrokePath(context);
    
    //生成新的image
    
    UIImage *newimg = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return newimg;
    
}

注意:如果使用UIGraphicsBeginImageContext(image.size);会导致图片有点失真模糊,可以采用 UIGraphicsBeginImageContextWithOptions(image.size, NO, [[UIScreen mainScreen] scale]);还有如果在视图上进行画圆,只有在视图没有其它盖住时才看得到,比如如果有个背景图片,那么它就没盖住了;

 3:在普通视图控制器包一个UINavigation

#pragma mark 添加导航栏
-(void)addNavigationBar{
    //创建一个导航栏
    UINavigationBar *navigationBar=[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, 320, 44+20)];
    //navigationBar.tintColor=[UIColor whiteColor];
    [self.view addSubview:navigationBar];
    //创建导航控件内容
    UINavigationItem *navigationItem=[[UINavigationItem alloc]initWithTitle:@"Web Chat"];
    
    //左侧添加登录按钮
    UIBarButtonItem *loginButton=[[UIBarButtonItem alloc]initWithTitle:@"登录" style:UIBarButtonItemStyleDone target:self action:@selector(login)];
    
    navigationItem.leftBarButtonItem=loginButton;
    
    //添加内容到导航栏
    [navigationBar pushNavigationItem:navigationItem animated:NO];
}

 4:系统自带定位坐标转为城市名

    //系统自带定位
    [[MPLocationManager shareInstance] startSystemLocationWithRes:^(CLLocation *loction, NSError *error) {
        if (!error) {
            CLGeocoder *geocoder=[[CLGeocoder alloc]init];
            [geocoder reverseGeocodeLocation:loction completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
                if (placemarks.count>0) {
                    CLPlacemark *placemark=[placemarks objectAtIndex:0];
                    //获取城市
                    NSString *city = placemark.locality;
                    if (!city) {
                        //四大直辖市的城市信息无法通过locality获得,只能通过获取省份的方法来获得(如果city为空,则可知为直辖市)
                        city = placemark.administrativeArea;
                    }
                    //有差异才改变
                    if(![BBUserDefault.LocationCity isEqualToString:city])
                    {
                        BBUserDefault.LocationCity=city;
                    }
                    
                    NSLog(@"当前城市:[%@]",city);
                }
            }];
        }
        BBUserDefault.latiude=[NSString stringWithFormat:@"%f",loction.coordinate.latitude];
        BBUserDefault.longitude=[NSString stringWithFormat:@"%f",loction.coordinate.longitude];
        NSLog(@"定位信息:[%f,%f]",loction.coordinate.latitude,loction.coordinate.longitude);
    }];

注意:MPLocationManager.h类的代码如下:

#import <Foundation/Foundation.h>

typedef void(^KSystemLocationBlock)(CLLocation *loction, NSError *error);

@interface MPLocationManager : NSObject


+ (id)shareInstance;

/**
 *  启动系统定位
 *
 *  @param systemLocationBlock 系统定位成功或失败回调成功
 */
- (void)startSystemLocationWithRes:(KSystemLocationBlock)systemLocationBlock;



@end
//
//  MPLocationManager.m
//  MobileProject
//
//  Created by wujunyang on 16/1/15.
//  Copyright © 2016年 wujunyang. All rights reserved.
//

#import "MPLocationManager.h"

@interface MPLocationManager()<CLLocationManagerDelegate>
@property (nonatomic, readwrite, strong) CLLocationManager *locationManager;

@property (nonatomic, readwrite, copy) KSystemLocationBlock kSystemLocationBlock;
@end

@implementation MPLocationManager

+ (id)shareInstance{
    static id helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        helper = [[MPLocationManager alloc]  init];
    });
    return helper;
}


#pragma mark - 苹果
/**
 *  苹果系统自带地图定位
 */
- (void)startSystemLocationWithRes:(KSystemLocationBlock)systemLocationBlock{
    self.kSystemLocationBlock = systemLocationBlock;
    
    if(!self.locationManager){
        self.locationManager =[[CLLocationManager alloc] init];
        self.locationManager.desiredAccuracy=kCLLocationAccuracyBest;
        //        self.locationManager.distanceFilter=10;
        if ([UIDevice currentDevice].systemVersion.floatValue >=8) {
            [self.locationManager requestWhenInUseAuthorization];//使用程序其间允许访问位置数据(iOS8定位需要)
        }
    }
    self.locationManager.delegate=self;
    [self.locationManager startUpdatingLocation];//开启定位
}

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
    CLLocation *currLocation=[locations lastObject];
    self.locationManager.delegate = nil;
    [self.locationManager stopUpdatingLocation];
    
    self.kSystemLocationBlock(currLocation, nil);
}
/**
 *定位失败,回调此方法
 */
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
    if ([error code]==kCLErrorDenied) {
        NSLog(@"访问被拒绝");
    }
    if ([error code]==kCLErrorLocationUnknown) {
        NSLog(@"无法获取位置信息");
    }
    self.locationManager.delegate = nil;
    [self.locationManager stopUpdatingLocation];
    
    self.kSystemLocationBlock(nil, error);
}
@end

 5:初始化init传参的一些样例

@implementation ZOCEvent

- (instancetype)initWithTitle:(NSString *)title
                         date:(NSDate *)date
                     location:(CLLocation *)location
{
    self = [super init];
    if (self) {
        _title    = title;
        _date     = date;
        _location = location;
    }
    return self;
}

- (instancetype)initWithTitle:(NSString *)title
                         date:(NSDate *)date
{
    return [self initWithTitle:title date:date location:nil];
}

- (instancetype)initWithTitle:(NSString *)title
{
    return [self initWithTitle:title date:[NSDate date] location:nil];
}

@end

 

IOS开发基础知识--碎片38

标签:

原文地址:http://www.cnblogs.com/wujy/p/5446624.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!