gpt4 book ai didi

ios - 如何快速从 UIImage 获取图像扩展名/格式?

转载 作者:可可西里 更新时间:2023-10-31 23:56:13 32 4
gpt4 key购买 nike

我正在尝试将图片上传到我的服务器,但在上传之前,我需要检查它是否是有效格式。

假设我只想要 .jpeg.png,所以如果用户从他们的手机中选择 .gif 格式的图像,我会显示一些警报。

我从用户图库/相机中获取图片,然后我想查看格式,但我不知道如何查看格式

@IBAction func selectPictureButtonDidPressed(_ sender: Any) {
let imagePickerController = UIImagePickerController()
imagePickerController.delegate = self
imagePickerController.allowsEditing = true

let actionSheet = UIAlertController(title: "Photo Source", message: "please choose your source", preferredStyle: .actionSheet)

// action camera
let actionCamera = UIAlertAction(title: "Camera", style: .default) { (action) in
if UIImagePickerController.isSourceTypeAvailable(.camera) {
imagePickerController.sourceType = .camera
self.present(imagePickerController, animated: true, completion: nil)
} else {
self.showAlert(alertTitle: "Opppss", alertMessage: "camera can't be used / not available", actionTitle: "OK")
print("camera can't be used / not available")
}
}

// action photo library
let actionPhotoLibrary = UIAlertAction(title: "Photo Library", style: .default) { (action) in
imagePickerController.sourceType = .photoLibrary
self.present(imagePickerController, animated: true, completion: nil)
}

//action cancel
let actionCancel = UIAlertAction(title: "Cancel", style: .cancel, handler: nil)

actionSheet.addAction(actionCamera)
actionSheet.addAction(actionPhotoLibrary)
actionSheet.addAction(actionCancel)

self.present(actionSheet, animated: true, completion: nil)
}

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
let image = info[UIImagePickerControllerOriginalImage] as! UIImage
postImage.image = image
picker.dismiss(animated: true, completion: nil)

doesItHasImage = true
}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}

我如何检查该格式?

我试图在谷歌上找到,但如果它是从 URL 派生的,我只能得到图像格式。不是像这样直接来自 UIImage

import UIKit
import ImageIO

struct ImageHeaderData{
static var PNG: [UInt8] = [0x89]
static var JPEG: [UInt8] = [0xFF]
static var GIF: [UInt8] = [0x47]
static var TIFF_01: [UInt8] = [0x49]
static var TIFF_02: [UInt8] = [0x4D]
}

enum ImageFormat{
case Unknown, PNG, JPEG, GIF, TIFF
}

extension NSData{
var imageFormat: ImageFormat{
var buffer = [UInt8](repeating: 0, count: 1)
self.getBytes(&buffer, range: NSRange(location: 0,length: 1))
if buffer == ImageHeaderData.PNG
{
return .PNG
} else if buffer == ImageHeaderData.JPEG
{
return .JPEG
} else if buffer == ImageHeaderData.GIF
{
return .GIF
} else if buffer == ImageHeaderData.TIFF_01 || buffer == ImageHeaderData.TIFF_02{
return .TIFF
} else{
return .Unknown
}
}
}

// USAGE
let imageURLFromParse = NSURL(string : "/image/R64uj.jpg")
let imageData = NSData(contentsOf: imageURLFromParse! as URL)
print(imageData!.imageFormat)

最佳答案

你可以试试这个:

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any])
{
let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
if (assetPath.absoluteString?.hasSuffix("JPG"))! {
print("JPG")
}
else if (assetPath.absoluteString?.hasSuffix("PNG"))! {
print("PNG")
}
else if (assetPath.absoluteString?.hasSuffix("GIF"))! {
print("GIF")
}
else {
print("Unknown")
}
}

关于ios - 如何快速从 UIImage 获取图像扩展名/格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47916207/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com