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

原创 2015年07月08日 15:34:15

//

//  ViewController.m

//  070802blueserver

//

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

//  Copyright (c) 2015 tianshangrenjian. All rights reserved.

//


#import "ViewController.h"

#import <MultipeerConnectivity/MultipeerConnectivity.h>

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

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

@property (strong,nonatomic) MCSession *session;

@property (strong, nonatomic) MCAdvertiserAssistant *assistant;

@property (strong, nonatomic) UIImagePickerController *imgPicker;


@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-server"];

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

    session.delegate=self;

    self.session=session;

    

    

    MCAdvertiserAssistant *assistant=[[MCAdvertiserAssistant alloc] initWithServiceType:@"sch-server" discoveryInfo:nil session:session];

    

    self.assistant=assistant;

    

}



- (void)dealloc

{

    [self.assistant stop ];

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}


- (IBAction)btStart:(id)sender {

    [self.assistant start];

    

}


- (IBAction)btPic:(id)sender {

    

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

    self.imgPicker=iPc;

    iPc.delegate=self;

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

    

}


- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)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];

    

    

    [picker dismissViewControllerAnimated:YES completion:nil];

}

- (void)imagePickerControllerDidCancel:(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从零开始学习socket编程——HTTP1.0服务器端

在前一篇文章《iOS从零开始学习socket编程——HTTP1.0客户端》中已经简单的介绍过了Socket编程和一些基本原理。并且实现了简单的iOS客户端(原文地址:http://blog.csdn....

{网络编程}和{多线程}应用:基于TCP协议【实现多个客户端发送文件给一个服务器端】--练习

{网络编程}和{多线程}应用: 基于TCP协议【实现多个客户端发送文件给一个服务器端】--练习...

java网络编程实现客户端连接服务器端,并发送消息例子(1)。(阻塞式的方法)

昨天晚上学了网络线程,并写了一个,客户端连接服务器的简单例子,有点QQ的感觉,但是实际上还差远,差不多就是很久之前那种“长江长江我是黄河”的通话形式。 代码如下: 服务器端代码:import ja...

《java入门第一季》之tcp协议下的编程实现键盘录入数据不断地往服务器端发送数据案例

这里要封装通道流,封装键盘录入数据流。 同样地,还是两台电脑。一个客户端,一个服务器。 客户端代码: import java.io.BufferedReader; import java.io...

ios推送php服务器端

  • 2012年12月28日 15:25
  • 2KB
  • 下载

在服务器端更新闪屏页图片

  • 2016年09月20日 14:51
  • 24.16MB
  • 下载

服务器端提供下载图片接口事例

/** * 将二进制转换为图片 * @param base64String */ public static File base64StringToImage(String base64...

andriod、iOS服务器端代码

  • 2015年04月14日 09:48
  • 1.09MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:iOS MultipeerConnectivity 编程发送图片服务器端
举报原因:
原因补充:

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