iOS MultipeerConnectivity 编程发送图片客户端

原创 2015年07月08日 15:50:35

//

//  ViewController.m

//  070803blueclient

//

//  Created by tianshangrenjian on 15/7/8.

//  Copyright © 2015 tianshangrenjian. All rights reserved.

//


#import "ViewController.h"

#import <MultipeerConnectivity/MultipeerConnectivity.h>


@interface ViewController ()<MCSessionDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate,MCBrowserViewControllerDelegate>

@property (weak, nonatomic) IBOutlet UIImageView *imgView;


@property (strong,nonatomic) MCSession *session;

@property (strong,nonatomic) UIImagePickerController *imgPc;

@property (strong,nonatomic) MCBrowserViewController *browser;





- (IBAction)btnFind:(id)sender;

- (IBAction)btnSelect:(id)sender;


@end


@implementation ViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view, typically from a nib.

    MCPeerID *peer=[[MCPeerID alloc] initWithDisplayName:@"sch-client"];

    MCSession *session=[[MCSession alloc] initWithPeer:peer];

    session.delegate=self;

    self.session=session;

    

    

    

}


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)btnFind:(id)sender {

    

    MCBrowserViewController *browser=[[MCBrowserViewController alloc] initWithServiceType:@"sch-server" session:self.session];

    self.browser=browser;

    browser.delegate=self;

    [self presentViewController:browser animated:YES completion:nil];

    

    

    

}


- (void)browserViewControllerDidFinish:(nonnull MCBrowserViewController *)browserViewController

{

    NSLog(@"select finded!");

    [browserViewController dismissViewControllerAnimated:YES completion:nil];

    

}

- (void)browserViewControllerWasCancelled:(nonnull MCBrowserViewController *)browserViewController

{

    NSLog(@"cancel select");

    

    [browserViewController dismissViewControllerAnimated:YES completion:nil];

}

- (IBAction)btnSelect:(id)sender {

    UIImagePickerController *iPc=[[UIImagePickerController alloc] init];

    self.imgPc=iPc;

    self.imgPc.delegate=self;

    [self presentViewController:self.imgPc animated:YES completion:nil];

    

}


- (void)imagePickerController:(nonnull UIImagePickerController *)picker didFinishPickingMediaWithInfo:(nonnull NSDictionary<NSString *,id> *)info

{

    UIImage *img=info[UIImagePickerControllerOriginalImage];

    self.imgView.image=img;

    NSData *data=UIImagePNGRepresentation(img);

    [self.session sendData:data toPeers:[self.session connectedPeers] withMode:MCSessionSendDataUnreliable error:nil];

    NSLog(@"begin send img...");

    [picker dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(nonnull UIImagePickerController *)picker

{

    [picker dismissViewControllerAnimated:YES completion:nil];

}













// Remote peer changed state.

- (void)session:(MCSession *)session peer:(MCPeerID *)peerID didChangeState:(MCSessionState)state

{

    switch (state) {

        case MCSessionStateConnecting:

            NSLog(@"connectiing!");

            break;

        case MCSessionStateConnected:

            NSLog(@"connected!");

        default:

            break;

    }

}


// Received data from remote peer.

- (void)session:(MCSession *)session didReceiveData:(NSData *)data fromPeer:(MCPeerID *)peerID

{

    NSLog(@"receive data");

    UIImage *img=[UIImage imageWithData:data];

    self.imgView.image=img;

}


// Received a byte stream from remote peer.

- (void)    session:(MCSession *)session

   didReceiveStream:(NSInputStream *)stream

           withName:(NSString *)streamName

           fromPeer:(MCPeerID *)peerID

{

    

}


// Start receiving a resource from remote peer.

- (void)                    session:(MCSession *)session

  didStartReceivingResourceWithName:(NSString *)resourceName

                           fromPeer:(MCPeerID *)peerID

                       withProgress:(NSProgress *)progress

{

    

}


// Finished receiving a resource from remote peer and saved the content

// in a temporary location - the app is responsible for moving the file

// to a permanent location within its sandbox.

- (void)                    session:(MCSession *)session

 didFinishReceivingResourceWithName:(NSString *)resourceName

                           fromPeer:(MCPeerID *)peerID

                              atURL:(NSURL *)localURL

                          withError:(NSError *)error

{

    

}



@end


iOS MultipeerConnectivity 编程发送图片服务器端

// //  ViewController.m //  070802blueserver // //  Created by tianshangrenjian on 15/7/...
  • woshidaniu
  • woshidaniu
  • 2015年07月08日 15:34
  • 302

iOS- 蓝牙集成GameKit,MultipeerConnectivity,CoreBluetooth

参考博客http://www.cnblogs.com/kenshincui/p/4220402.html#bluetooth 忙中偷闲,看到上面博客不错,正想没接触过蓝牙,话说好记性不如烂笔头,于是参...
  • chenglei9128
  • chenglei9128
  • 2015年04月21日 10:53
  • 602

MultipeerConnectivity 点对点连接

在IOS7中,介绍了一个全新的框架
  • eric_jing
  • eric_jing
  • 2014年09月18日 11:35
  • 5382

iOS开发MultipeerConnectivity(多点连接)

MultipeerConnectivity 在iOS7中,引入了一个全新的框架——Multipeer Connectivity。 利用Multipeer Connectivity框架,...
  • z2340868
  • z2340868
  • 2016年09月09日 00:38
  • 644

蓝牙框架之MultipeerConnectivity框架

蓝牙框架之MultipeerConnectivity框架 前面已经说了GameKit相关的蓝牙操作类从iOS7已经全部过期,苹果官方推荐使用MultipeerConnectivity代替。但是应...
  • weixin_35755389
  • weixin_35755389
  • 2017年01月01日 10:46
  • 114

MultipeerConnectivity连接蓝牙设备

#import /*     1. 注册一个广告  告诉别人  我的设备是可以被发现     2. 扫描蓝牙设备   需要实现代理方法     3. 使用一个MCSession对象存储当前会...
  • BianHuanShiZhe
  • BianHuanShiZhe
  • 2017年02月07日 17:48
  • 196

编写一个服务端可以给多个客户端发送图片

package cn.itcast.tcp;import java.io.FileInputStream; import java.io.FileNotFoundException; import j...
  • AaronWang0920
  • AaronWang0920
  • 2015年09月09日 14:27
  • 658

iOS-MultipeerConnectivity框架开发(一)

英文原文:Understanding Multipeer Connectivity Framework in iOS 7 – Part 1 Multipeer Connectivity Fr...
  • u014220518
  • u014220518
  • 2016年03月07日 15:26
  • 778

IOS网络篇7之基于第三方CocoaAsyncSocket实现Socket通信(发送图片以及类似http头信息)

紧接这一篇 IOS(5)网络篇5之IOS底层网络架构概述
  • u014011807
  • u014011807
  • 2014年10月09日 17:36
  • 1230

TCP练习1:服务器端读取图片并发送给客户端,客户端保存图片到本地

import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import...
  • motianlundejiyi
  • motianlundejiyi
  • 2015年05月08日 14:28
  • 1036
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS MultipeerConnectivity 编程发送图片客户端
举报原因:
原因补充:

(最多只允许输入30个字)