这一篇博文讲述发微博界面的实现。

首先我们先了解一下在这个发微博界面中需要做哪些事情吧!

(1) 发微博包括文字内容和微博图片,所以我们可以用一个textview来装载微博文字内容,用一个imageview来装载图片内容。

①在文字部分,用一个textview,在发送的时候检测一下发送文字的个数,如果超过140,那么给出提示信息。在图片部分,用一个imageview,并且如果添加了图片,那么在图片的右上角添加一个打叉的按钮,作用是去除图片;当然,在你没有选择添加图片或者取消了已选图片时,按键自动取消。效果如下图:

这个打叉的cancelButton的处理比较简单,代码如下:

[cpp] view plaincopy
  1. - (IBAction)cancelImage:(id)sender {
  2. _cancelButton.hidden = YES;
  3. self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];
  4. }

在导航栏的右边添加一个发送按键,处理相关的发送信息。代码如下:

[cpp] view plaincopy
  1. - (IBAction)sendWeibo:(id)sender {
  2. [self.theTextView resignFirstResponder];
  3. NSString *content = [[NSString alloc] initWithString:_theTextView.text];
  4. //计算发送微博的内容字数,并作相应的处理
  5. NSInteger contentLength = content.length;
  6. if (contentLength > 140) {
  7. MBProgressHUD *overLengthHud = [[MBProgressHUD alloc] initWithView:self.view];
  8. [self.view addSubview:overLengthHud];
  9. overLengthHud.mode = MBProgressHUDModeText;
  10. overLengthHud.labelText = @"提示信息";
  11. overLengthHud.detailsLabelText = [NSString stringWithFormat:@"微博字数:%d 超过140上限!",contentLength];
  12. [overLengthHud show:YES];
  13. [overLengthHud hide:YES afterDelay:2];
  14. }
  15. else {
  16. UIImage *image = _theImageView.image;
  17. //没有图片
  18. if (!hasPostImage) {
  19. [self postWithText:content];
  20. }
  21. //有图片
  22. else {
  23. [self postWithText:content image:image];
  24. }
  25. hud = [[MBProgressHUD alloc] init];
  26. hud.dimBackground = YES;
  27. hud.labelText = @"正在发送...";
  28. [hud show:YES];
  29. [self.view addSubview:hud];
  30. }
  31. }

注意到其中的方法 -(void) postWithText:(NSString*)text 是发单纯文字微博的,而方法 -(void)postWithText:(NSString *)text image:(UIImage*)image 是发文字+图片微博的。具体的实现过程下面会讲解。

修改:突然发现其实上面的发送代码中还存在一个小问题,假如其中不输入文字也可以发送,显然这是不对的。那么还有添加一个if判断一下字数长度是否为0,如果是0的话给出一个alert窗口提示一下。

[cpp] view plaincopy
  1. if (contentLength == 0) {
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"请输入微博内容!" delegate:nil cancelButtonTitle:@"好" otherButtonTitles:nil, nil];
  3. [alert show];
  4. }
  5. else if (contentLength > 140) {
  6. }
  7. else {
  8. }

②这里要着重说的是如何添加图片。

这里用一个按键用于插入图片。

插入的图片来源包括系统相册和拍摄。

在这里我们需要用到UIImagePickerController 类来创建图像选取器。UIImagePickerController 图像选取器是一种导航控制器类,让你可以在应用程序中添加简单的图像选择功能或者照相机界面。用户会看到一个图像选择屏幕,在其中挑选相片,相片的来源则是他自己的相片库、保存下来的相片集或者照相机。当用户选定一个相片后,就会通过 UIImagePickerDelegate 协议中的方法,通知选取器的委托方法实现图像的选取。

The role and appearance of an image picker controller depend on the source type you assign to it before you present it.

A sourceType of UIImagePickerControllerSourceTypeCamera provides a user interface for taking a new picture or movie (on devices that support media capture).

A sourceType of UIImagePickerControllerSourceTypePhotoLibrary or UIImagePickerControllerSourceTypeSavedPhotosAlbum provides a user interface for choosing among saved pictures and movies.

这里说明了通过sourceType 设定图像的来源。包括

enum{

UIImagePickerControllerSourceTypePhotoLibrary,//相片库

UIImagePickerControllerSourceTypeCamera,//照相机

UIImagePickerControllerSourceTypeSavedPhotosAlbum//保存的相片

};

typedef NSUInteger UIImagePickerControllerSourceType;

Verify that the device is capable of picking content from the desired source. Do this calling the isSourceTypeAvailable: class method, providing a constant from the “UIImagePickerControllerSourceType” enumeration.

在使用时应先检查当前设备是否支持使用UIImagePickerController,这个时候我们需要调用isSourceTypeAvailable:方法判断,需要提供sourceType 作为参数

当用户选择一个图片之后,选择器的委托会通过 didFinishPickingImage 方法接到通知。代理会得到一个包含有该图像的 UIImage 对象,如果编辑功能开启的话,还会得到一个包含了编辑属性的NSDictionary。

使用UIImagePickerController的时候注意添加这两个delegate,<UIImagePickerControllerDelegate,UINavigationControllerDelegate>并实现其中的两个方法。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

设置选取器的delegate ,就可以将一个委托赋予选择器:picker.delegate =self;

在这里我们需要实现下面的这个第一个方法,这样当选取一个图像时,委托类就会得到通知,在这个方法中添加图像处理的有关代码就可以实现对选取图片的处理。

添加图片这部分的代码如下:

[cpp] view plaincopy
  1. - (IBAction)addPhoto:(id)sender {
  2. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"插入图片" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"系统相册",@"拍摄", nil];
  3. [alert show];
  4. }
  5. #pragma mark - UIAlertViewDelegate
  6. - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
  7. {
  8. if (buttonIndex == 1)
  9. {
  10. [self addPhoto];
  11. }
  12. else if(buttonIndex == 2)
  13. {
  14. [self takePhoto];
  15. }
  16. }
  17. - (void)addPhoto
  18. {
  19. UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
  20. imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  21. imagePickerController.delegate = self;
  22. imagePickerController.allowsEditing = NO;
  23. [self presentModalViewController:imagePickerController animated:YES];
  24. }
  25. - (void)takePhoto
  26. {
  27. if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  28. {
  29. UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
  30. message:@"该设备不支持拍照功能"
  31. delegate:nil
  32. cancelButtonTitle:nil
  33. otherButtonTitles:@"好", nil];
  34. [alert show];
  35. }
  36. else
  37. {
  38. UIImagePickerController * imagePickerController = [[UIImagePickerController alloc]init];
  39. imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
  40. imagePickerController.delegate = self;
  41. imagePickerController.allowsEditing = NO;
  42. [self presentModalViewController:imagePickerController animated:YES];
  43. }
  44. }
  45. #pragma mark - UIImagePickerControllerDelegate
  46. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
  47. {
  48. [picker dismissModalViewControllerAnimated:YES];
  49. UIImage * image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
  50. self.theImageView.image = image;
  51. hasPostImage = YES;
  52. _cancelButton.hidden = NO;
  53. }

(1) 发微博需要用到的API调用;如果只是发文字微博的话,是用到这个API:https://api.weibo.com/2/statuses/update.json;如果是发文字+图片微博的话,用到的是这个API:https://upload.api.weibo.com/2/statuses/upload.json。注意到二者的HTTP请求都是POST;而且请求参数中也要做一点出来,文字的话必须做URLencode,内容不超过140个汉字;图片的话,需要是binary类型的,而且仅支持JPEG 、GIF 、PNG格式,图片大小小于5M。

这部分数据请求的处理我用到了ASIHTTPRequest这个第三方类库。

处理的代码如下:

[cpp] view plaincopy
  1. //发布文字微博
  2. -(void) postWithText:(NSString*)text {
  3. NSURL *url = [NSURL URLWithString:WEIBO_UPDATE];
  4. ASIFormDataRequest *item = [[ASIFormDataRequest alloc] initWithURL:url];
  5. [item setPostValue:[InfoForSina returnAccessTokenString]    forKey:@"access_token"];
  6. [item setPostValue:text                                     forKey:@"status"];
  7. [item setCompletionBlock:^{
  8. self.theTextView.text = nil;
  9. [hud removeFromSuperview];
  10. MBProgressHUD *custuonHUD = [[MBProgressHUD alloc]initWithView:self.view];
  11. custuonHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
  12. custuonHUD.labelText = @"发微博成功!";
  13. custuonHUD.mode = MBProgressHUDModeCustomView;
  14. [self.view addSubview:custuonHUD];
  15. [custuonHUD show:YES];
  16. [custuonHUD hide:YES afterDelay:1];
  17. }];
  18. [item startAsynchronous];
  19. }
  20. //发布文字图片微博
  21. -(void)postWithText:(NSString *)text image:(UIImage*)image {
  22. NSURL *url = [NSURL URLWithString:WEIBO_UPLOAD];
  23. ASIFormDataRequest *item = [[ASIFormDataRequest alloc] initWithURL:url];
  24. [item setPostValue:[InfoForSina returnAccessTokenString]    forKey:@"access_token"];
  25. [item setPostValue:text                                     forKey:@"status"];
  26. [item addData:UIImagePNGRepresentation(image)               forKey:@"pic"];
  27. [item setCompletionBlock:^{
  28. self.theImageView.image = [UIImage imageNamed:@"noImage50x118.png"];
  29. self.theTextView.text = nil;
  30. [hud removeFromSuperview];
  31. _cancelButton.hidden = NO;
  32. MBProgressHUD *custuonHUD = [[MBProgressHUD alloc]initWithView:self.view];
  33. custuonHUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
  34. custuonHUD.labelText = @"发微博成功!";
  35. custuonHUD.mode = MBProgressHUDModeCustomView;
  36. [self.view addSubview:custuonHUD];
  37. [custuonHUD show:YES];
  38. [custuonHUD hide:YES afterDelay:1];
  39. }];
  40. [item startAsynchronous];
  41. }

使用时需要#import "ASIFormDataRequest.h"

有两点需要说明的是:

1、前面已经说到文字内容需要URLencode,在这里我么并没有做什么处理,因为我们使用的这个第三方类库已经帮我们实现了;对于微博图片内容,必须转换成binary现实,我们是这样处理的:UIImagePNGRepresentation(image)

NSData * UIImagePNGRepresentation (
   UIImage *image
);

Returns the data for the specified image in PNG format

2、在发送完成的block中我们对textview和imagview都做了处理,方便发送下一条微博;同时我添加了一个提示框提示发送成功的信息,在这个提示框中我使用了customView,也就是打钩的image。效果图如下。

好了,大概就这样说完了!

转载于:https://www.cnblogs.com/yulang314/p/3549547.html

ios sinaweibo 客户端(二)相关推荐

  1. ios sinaweibo 客户端(三)

    这个页面要讲述的是用户的粉丝列表,下面是效果图: 可以看到这个视图明显也是一个tableview,在每一个cell中包含的有三个部分的内容:粉丝头像image,粉丝昵称label,我和粉丝之间的相互关 ...

  2. ios sinaweibo 客户端(一)

    上一篇sina微博Demo已经完成的认证,下面就开始进入微博相关内容的加载及显示.其实主要的工作就是调用微博API 加载相关的json数据,然后进行解析,然后在界面中进行组织好在tableview中进 ...

  3. 除草机(Grasscutter) ios/Android客户端配置教程

    本文章仅为Grasscutter的ios/Android客户端配置教程,不提供任何服务器,如果你按图中的数据连上了,纯属瞎猫碰到死耗子. 对应的客户端配置教程在:从零开始,一镜到底,纯净系统搭建除草机 ...

  4. 基于XMPP的IOS聊天客户端程序(XMPP服务器架构)

    最近看了关于XMPP的框架,以文本聊天为例,需要发送的消息为: <message type="chat" from="kang@server.com" t ...

  5. [iPhone高级] 基于XMPP的IOS聊天客户端程序(XMPP服务器架构)

    最近看了关于XMPP的框架,以文本聊天为例,需要发送的消息为: [html] view plaincopy <message type="chat" from="k ...

  6. iOS原生实现二维码扫描

    iOS原生实现二维码扫描 最近项目上需要开发扫描二维码进行签到的功能,主要用于开会签到的场景,所以为了避免作弊,我们再开发时只采用直接扫描的方式,并且要屏蔽从相册读取图片,此外还在二维码扫描成功签到时 ...

  7. iOS 苹果手机客户端微信支付调起失败--无法调起微信的原因

    1. iOS 苹果手机客户端微信支付调起失败–无法调起微信的原因 微信的SDK分两种,一种包含支付的SDK,另一中是不包含支付的SDK, 因为项目早期集成的是微信分享的SDK,不带支付的功能,所以更换 ...

  8. android iOS App客户端如何实现在线支付

    *android iOS App客户端如何实现在线支付**Q~~1⑨9⑦O*⑦46 阔别已久,小课堂再次开课,今天将和大家分享在开通各个支付渠道之前,你可能想要了解的一些信息. 对支付领域一无所知,对 ...

  9. ios内购二次验证安全性问题_苹果IOS内购二次验证返回state为21002的坑

    项目是三四年前的老项目,之前有IOS内购二次验证的接口,貌似很久都没用了,然而最近IOS的妹子说接口用不了,让我看看啥问题.接口流程时很简单的,就是前端IOS在购买成功之后,接收到receipt后进行 ...

  10. iOS相册图片二维码识别

    前言:最近客户要求开发一个功能,类似微信长按图片识别图片中的二维码,一开始我使用了ZXingObjC,但是完成后被测试出有些二维码识别不了,所以只能另寻它法,之后更换为苹果系统自带的识别图片二维码的功 ...

最新文章

  1. Swift -布局框架SnapKit使用
  2. Nature癌症“牵线木偶”理论:科学家找到了不易误伤健康细胞的“剪刀”
  3. idea集成python_IDEA集成Python插件,SDK配置
  4. Spring MVC与Struts2对比
  5. memcached php封装类,PHP Memcached + APC + 文件缓存封装_PHP - key
  6. 用于制作app store的截图的工具:Brief Wrapper —— 最便捷的应用商店屏幕快照
  7. 硬盘整数分区计算方法一般算法
  8. PMP项目管理13个计划
  9. Origin软件使用TIPS
  10. 联想E431 win10+Unbutun+Deepin系统安装
  11. 9、recoil库的基本使用
  12. IM群聊消息的已读未读功能在存储空间方面的实现思路探讨
  13. 高等数学——积分中值定理
  14. response导出html到word
  15. 西安思源中学2021高考成绩查询入口,2021年西安各高中高考成绩排名及放榜最新消息...
  16. IDEA中搜索 matches limit 默认显示100,调整限制,增加搜索返回数量
  17. 工程伦理 第三章习题 答案
  18. jsp+ssm计算机毕业设计游戏装备交易网站论文2022【附源码】
  19. 国家开放大学-乡村社会学-形考作业2
  20. mips汇编代码示例解释_通过示例解释cosmosdb

热门文章

  1. 『C#基础作业』4.类的静态成员示例
  2. Query with 0 value even no record found
  3. 工作室招新管理系统需求分析
  4. 阅读之大量数据访问机器的架构优化
  5. 用AngularJS开发下一代Web应用pdf
  6. 【转】VMware网络连接模式—桥接、NAT以及仅主机模式的详细介绍和区别
  7. 信息安全系统设计基础实验二:固件设计
  8. 杨森翔的书法-10斗方:杜牧中秋月
  9. 挣脱浏览器的束缚(6) - AJAX也跨域名
  10. LVS 三种工作模式