打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
37、添加一张图片到相册内部
1、当点击工具条上方的图片、相册按钮时,应当会由响应的操作。

2、相关代码如下:

/**打开照相机*/

- (void)openCamera

{

    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) return;

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

    ipc.sourceType = UIImagePickerControllerSourceTypeCamera;

    ipc.delegate = self;

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

}

打开相册和打开相机的代码类似。


2、UIImagePickerController类可以打开相册,相机。这个类比较特殊,需要遵守两个协议。
这里发微博控制器遵守该协议。UINavigationControllerDelegate, UIImagePickerControllerDelegate。

3、当点击相册,相机的按钮时,会调用UIImagePickerController代理的方法。

#pragma mark - UIImagePickerControllerDelegate

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

{

    [picker dismissViewControllerAnimated:YES completion:nil];    

    // 1.取出选中的图片  返回的是字典,通过key取到图片。

    UIImage *image = info[UIImagePickerControllerOriginalImage];

    // 2.添加图片到相册中

    [self.photosView addImage:image];

}


4、当点击相册按钮时,来到UIImagePickerController代理的方法。在代理方法中,添加[self.photosView addImage:image];拿到图片。这里的photosView是一个自定义的UIView( HMComposePhotosView)。HMComposePhotosView内部提供一个方法,- (void)addImage:(UIImage *)image。该方法将把照片给自定义的HMComposePhotosView。具体的图片排列操作,交给HMComposePhotosView处理。自定义控件的位置在layoutSubviews中进行排序。至于具体的处理过程,发微博控制类无须过多的关注。自定义的控件(HMComposePhotosView)提供一个方法,把照片给该自定义控件。自定义控件的内部位置排序,在layoutSubviews中设置。

- (void)addImage:(UIImage *)image

{

    UIImageView *imageView = [[UIImageView alloc] init];

    imageView.contentMode = UIViewContentModeScaleAspectFill;//

    imageView.clipsToBounds = YES;//有一定的剪切效果。

    imageView.image = image;

    [self addSubview:imageView];

}

6、该自定义的控件HMComposePhotosView,添加在自定义的HMTextView上的。因为HMTextView下拉,上拉操作,都会引起照片的运动。

// 添加显示图片的相册控件

- (void)setupPhotosView

{

    HMComposePhotosView *photosView = [[HMComposePhotosView alloc] init];

    photosView.width = self.textView.width;

    photosView.height = self.textView.height;

    photosView.y = 70;

    [self.textView addSubview:photosView];

    self.photosView = photosView;

}




本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
关于iOS中简单实现调用系统相机及相册功能
UIImagePickerController调用相册/相机
swift2.0 UIImagePickerController 拍照 相册 录像
一个iOS图片选择器的DEMO(实现图片添加,宫格排列,图片长按删除,以及图片替换等功能)
swift 从相册里取图片并Post传到服务器上
利用UIScrollView实现图片的放大、缩小和移动查看
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服