高新技术:二维码 & 摇一摇 & 分享

1. 二维码

iOS7之前使用第三番 Z-Xing Z-Bar
iOS7之后系统提供原生方式

  • 二维码的生成,通过一定的规则把数据转换成图片

      //1.创建一个种类的滤镜
    

***二维码
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
***条形码
CIFilter *filter1 = [CIFilter filterWithName:@"CICode128BarcodeGenerator"];
//2.恢复滤镜的默认设置(清除已经设置过的效果)
[filter setDefaults];

//3.将隐藏的地址变成二进制数据
NSData *data = [@"http://www.baidu.com" dataUsingEncoding:NSUTF8StringEncoding];

//4.通过KVC的方式设置滤镜,传入要隐藏的data
//滤镜就能够依据data生成二维码
[filter setValue:data forKey:@"inputMessage"];

//5.输出二维码图片
CIImage *outputImage = [filter outputImage];

//6.将CIImage转换成UIImage
UIImage *image = [UIImage imageWithCIImage:outputImage];

//7.显示
self.imageView.image = image;
- 扫描二维码
 1. 打开后置摄像头
 2. 从后置摄像头中读取数据输入流
 3. 把输入流输出到屏幕上---输出流
 4. 把输入流和输出流连接起来---管道session
 5. 让输出流实时过滤数据,监听是否有二维码(条形码)如果有就通过协议通知我们

import "ViewController.h"

import

@interface ViewController ()

  • (IBAction)startScan:(id)sender;
    /** 连接输入输出流的对象/
    @property (nonatomic,strong)AVCaptureSession * session;
    /
    * 用于展示输出流的对象*/
    @property (nonatomic,strong)AVCaptureVideoPreviewLayer * videoLayer;
    @end

@implementation ViewController

  • (void)viewDidLoad {
    [super viewDidLoad];
    }

  • (IBAction)startScan:(id)sender {
    //1.获取后置摄像头管理对象
    AVCaptureDevice *device = [AVCaptureDevice defaultDeviceWithMediaType:AVMediaTypeVideo];
    //2.获取输入流
    NSError *error = nil;
    AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:device error:&error];
    if (error) {
    NSLog(@"%@",error);
    }
    //3.创建输出流 (把图像显示)
    AVCaptureMetadataOutput *output = [AVCaptureMetadataOutput new];
    //设置代理
    [output setMetadataObjectsDelegate:self queue:dispatch_get_main_queue()];

    //4.构建连接通道
    _session = [AVCaptureSession new];
    [_session addInput:input];
    [_session addOutput:output];

    //5.通道质量设置(流畅 高清 标清)
    [_session setSessionPreset:AVCaptureSessionPresetHigh];

    //6.设置输出流的类型(二维码 条形码...)一定要在通道连接之后设置,否则不起作用
    output.metadataObjectTypes = @[AVMetadataObjectTypeQRCode,AVMetadataObjectTypeCode128Code,AVMetadataObjectTypeEAN8Code,AVMetadataObjectTypeEAN13Code];

    //7.显示
    _videoLayer = [AVCaptureVideoPreviewLayer layerWithSession:_session];
    _videoLayer.frame = self.view.bounds;

    [self.view.layer addSublayer:_videoLayer];

    //8.启动通道
    [self.session startRunning];
    }

pragma mark -AVCaptureMetadataOutputObjectsDelegate

//当扫描到数据时
-(void)captureOutput:(AVCaptureOutput *)captureOutput didOutputMetadataObjects:(NSArray *)metadataObjects fromConnection:(AVCaptureConnection *)connection{
if (metadataObjects.count > 0) {
[_session stopRunning];
[_videoLayer removeFromSuperlayer];
//拿出扫描数据
AVMetadataMachineReadableCodeObject *object = metadataObjects.firstObject;
NSLog(@"扫描到的数据是:%@",[object stringValue]);
}
}```

2. 摇一摇

  • 1 开始摇一摇
    -(void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event
  • 2 结束摇一摇
    -(void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event
  • 3 取消摇一摇(被中断,例如:来电)
    -(void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event

3. 社会化分享

  • 4.1 分享到各方平台。例如:分享到新浪微博->第三方登录->分享
  • 4.2 系统自带的分享
    ->iOS6之后集成新浪微博
    ->iOS7之后支持腾讯微博
#import "ViewController.h"
#import 
@interface ViewController ()

@end

@implementation ViewController

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    if (![SLComposeViewController isAvailableForServiceType:SLServiceTypeSinaWeibo]) {
        NSLog(@"不支持新浪微博");
    }
    //1.创建类型
    SLComposeViewController *cvc = [SLComposeViewController composeViewControllerForServiceType:SLServiceTypeSinaWeibo];
    //2.设置数据
    [cvc setInitialText:@"潮湿的不得了"];
    [cvc addImage:[UIImage imageNamed:@"icon"]];
    //3.显示
    [self presentViewController:cvc animated:YES completion:nil];
    //4.监听
    cvc.completionHandler = ^(SLComposeViewControllerResult result){
        if (result == SLComposeViewControllerResultDone) {
            NSLog(@"发布成功");
        }else{
            NSLog(@"发布失败");
        }
    };
}

@友盟分享

  • 1 注册并登陆
  • 2 新建应用获得appkey
  • 3 找到官方的Apple平台集成文档
如果运行在真机,程序崩溃 出现bitcode
 Target -> Build Setting ->修改BitCode```
@[添加微信分享](http://dev.umeng.com/social/ios/detail-share#2_1)->设置scheme->设置白名单

你可能感兴趣的:(高新技术:二维码 & 摇一摇 & 分享)