iOS:检测或禁用多个图像选择 [英] iOS: Detect or disable multiple image selection

查看:28
本文介绍了iOS:检测或禁用多个图像选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个带有图像附件控件的 UIWebview.

我们用户点击控件会弹出一个 iOS 操作表.

当用户点击浏览"或照片库"时,用户可以选择多张图片.

然后我拦截了

- (void)imagePickerController: didFinishPickingMediaWithInfo: 使用我自己的 ImagePickerViewController 调用.

那么,我的问题是:在 didFinishPickingMediaWithInfo 中,是否可以检测到已选择多个图像?如果没有,有没有办法强制iOS端的图片选择只允许一张图片?

如果可能,我想检测到多个图像已被选中,然后返回到正常功能(即我不会混用).

我确实检查了作为参数发送到 didFinishPickingMediaWithInfo 的选择器和信息,但即使选择了多张图像,信息参数也只有一张图像的信息.它保留第一个选择并丢弃第二个,并且不会第二次调用该方法.

我没有使用第三方图片选择器.

解决方案

UIImagePickerController 调用 didFinishPickingMediaWithInfo,然后调用 didFinishPickingMultipleMediaWithInfo.它首先调用 didFinishPickingMediaWithInfo,然后我检查

if info["UIImagePickerControllerReferenceURL"] == nil {//处理选中的单张图片,我们不想处理多张图片} 别的 {//没做什么}

然后 didFinishPickingMultipleMediaWithInfo 被调用,这次使用适当的数组,如果数组中的项数大于 1,我们使用正常方法而不是 swizzled 代码,但如果有只有一张图片,我们照常使用 swizzled 方法对其进行处理.

因此,我们无法禁用多张图片选择,但使用 didFinishPickingMultipleMediaWithInfo,我们可以检测何时选择了多张图片并更改应用程序的行为方式.

We have a UIWebview with an image attachment control.

We the user taps the control an iOS action sheet pops up.

When the user taps 'Browse' or 'Photo Library' the user can select multiple images.

I then intercept the

- (void)imagePickerController: didFinishPickingMediaWithInfo: call with my own ImagePickerViewController that has been swizzled in.

So, my question: In didFinishPickingMediaWithInfo, is it possible to detect that multiple images have been selected? If not, is there a way to force the image selection on the iOS side to only allow one image?

If possible I would like to detect that multiple images have been selected, and then fall back to normal functionality (i.e. I won't swizzle).

I did examine the picker and info that is sent to didFinishPickingMediaWithInfo as arguments, but even though multiple images are selected, the info parameter only has info on one image. It keeps the first selection and discards the second, and the method is not called a second time.

Edit: I'm not using a 3rd party image picker.

解决方案

The UIImagePickerController calls didFinishPickingMediaWithInfo and then calls didFinishPickingMultipleMediaWithInfo. It calls didFinishPickingMediaWithInfo first, I then check

if info["UIImagePickerControllerReferenceURL"] == nil {
    // process the selected single image, we don't want to process multiple images
} else {
    // do nothing
}

Then didFinishPickingMultipleMediaWithInfo is called, this time with the proper array, if the number of items in the array is greater than one, we use the normal methods and not the swizzled code, but if there is only one image, we process it with the swizzled methods as normal.

So, we could not disable multiple image selection, but using didFinishPickingMultipleMediaWithInfo, we can detect when multiple images are selected and change how the app behaves in return.

这篇关于iOS:检测或禁用多个图像选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆