zoukankan      html  css  js  c++  java
  • ios sinaweibo 客户端(二)

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


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

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

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

     

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

     

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

     

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

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

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

     

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

     

    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.     }  
    8.     else {  
    9.           
    10.     }  



     

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

     

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

     


     

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

    在这里我们需要用到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.

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

    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;

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

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

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

    (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这个第三方类库。

    处理的代码如下:

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

    使用时需要#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。效果图如下。



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

  • 相关阅读:
    健康检查详解:机制、配置、对比、实操
    制作自签名证书
    常用的UML建模
    UML建模更好的表达产品逻辑
    常用的UML建模
    UML建模图实战笔记
    领域驱动设计学习之路—DDD的原则与实践
    DDD领域驱动设计理论篇
    WAN、LAN、WLAN三种网口的区别
    新生代Eden与两个Survivor区的解释
  • 原文地址:https://www.cnblogs.com/yulang314/p/3549547.html
Copyright © 2011-2022 走看看