如何从UIImagePickerController获取图像文件名?

时间:2017-11-12 07:09:33

标签: ios iphone swift uiimagepickercontroller

我没有从照片库中获取图片名称。 这里的任何人都知道获取图像名称的正确方法吗?

注意:PHAsset.fetchAssets(withALAssetURLs: [imageURL], options: nil)这已被弃用。

仅限斯威夫特

由于

7 个答案:

答案 0 :(得分:7)

我找到了一种方式

serialize()

这将返回照片库中的实际文件名。

答案 1 :(得分:5)

你的意思是图像文件名吗?如果是这样,这应该对你有帮助。在你的UIImagePickerControllerDelegate中:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    guard let fileUrl = info[UIImagePickerControllerImageURL] as? URL else { return }
    print(fileUrl.lastPathComponent)
}

答案 2 :(得分:3)

尝试使用以下代码

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
    if let asset = info[UIImagePickerControllerPHAsset] as? PHAsset {
        if let fileName = (asset.value(forKey: "filename")) as? String {
          print(\(fileName))
        }
    }

    // Other Stuff
}

答案 3 :(得分:2)

正确的做法是:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
  var imageName = "image_1"
  if let asset = info[.phAsset] as? PHAsset, let fileName = asset.value(forKey: "filename") as? String {
    imageName = fileName
  } else if let url = info[.imageURL] as? URL {
    imageName = url.lastPathComponent
  }
}
如果用户未授予您的应用访问库的权限,则

PHAsset将是nil。如果您使用默认的UIImagePickerController,则不再需要此权限,因此,如果您的应用没有此权限,则操作系统将为您提供对图像副本的临时访问权,您可以做的只是退回到路径网址。

答案 4 :(得分:0)

如果您尚未授予访问照片库的权限,则

CXX = /usr/bin/g++ MOC = /home/user/programs/Qt5.11.2/5.11.2/gcc_64/bin/moc INCLUDES = \ -I/home/user/programs/Qt5.11.2/5.11.2/gcc_64/include \ -I/home/user/programs/Qt5.11.2/5.11.2/gcc_64/include/QtCore \ -I/home/user/programs/Qt5.11.2/5.11.2/gcc_64/mkspecs/linux-g++ \ -I/usr/include LINK_FLAGS = \ -Wl,-m,elf_x86_64,-rpath,/home/user/programs/Qt5.11.2/5.11.2/gcc_64/lib \ -L/home/user/programs/Qt5.11.2/5.11.2/gcc_64/lib \ -m64 /home/user/programs/Qt5.11.2/5.11.2/gcc_64/lib/libQt5Core.so.5.11.2 \ -lpthread C_FLAGS = \ -g \ -O0 \ -Wall \ -Wextra \ -m64 \ -pipe \ -fexceptions \ -fvisibility=default \ -fPIC \ -DQT_CORE_LIB \ $(INCLUDES) \ -std=c++11 SOURCES = main.cpp OBJS = $(SOURCES:%.cpp=%.cpp.o) HEADERS_THAT_NEED_MOC = MyQtClass.h MOC_OBJS = $(HEADERS_THAT_NEED_MOC:%.h=moc_%.cpp.o) all: HelloWorld-Qt HelloWorld-Qt: $(OBJS) $(MOC_OBJS) $(CXX) $^ $(LINK_FLAGS) -o $@ %.cpp.o : %.cpp $(CXX) $(C_FLAGS) -c $^ -o $@ moc_%.cpp: %.h $(MOC) -DQT_CORE_LIB $(INCLUDES) $^ -o $@ clean: rm -f *.cpp.o HelloWorld-Qt moc_*.cpp UIImagePickerControllerPHAsset。操作系统不会自动询问,仍然会允许您访问该文件(我不知道其背后的逻辑是什么!),但是PHAsset信息字典将为零!

答案 5 :(得分:0)

请尝试这个。

---
stim_proc  :PROCESS
begin
---

for Rd_loop in 0 to 7 loop -- 0 to 7 for Rd'lenght-1=2
    for Rs-Rt_loop in 0 to 7 loop -- 0 to 7 for Rs'lenght-1=Rt'lenght-1=2
        wait for 10 ns;

-- increment to the next value of Rs and Rt.

        Rs<=std_logic_vector( signed(Rs) + 1 );
        Rt<=std_logic_vector( signed(Rt) + 1 );
    end loop;

-- increment to the next value of Rd.
Rd<=std_logic_vector( signed(Rd) + 1 );
end loop;

        wait for 10 ns;

-----

答案 6 :(得分:0)

针对iOS 12,Swift 4的更新

这将直接为您提供文件名和文件扩展名

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

    guard let fileUrl = info[UIImagePickerController.InfoKey.imageURL] as? URL else { return }
    print(fileUrl.lastPathComponent) // get file Name
    print(fileUrl.pathExtension)     // get file extension
    dismiss(animated: true, completion: nil)
}