2017-10-07 60 views
0

我将我从画廊到它的URL,像这样采摘的图片问题与获取图像的URL在画廊

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

if let image = info[UIImagePickerControllerOriginalImage] as? UIImage { 
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(_:didFinishSavingWithError:contextInfo:)), nil) 

let imageURL = info[UIImagePickerControllerReferenceURL] as? NSURL 
let imageName = imageURL?.lastPathComponent 
let documentDirectory = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! 
let photoURL = NSURL(fileURLWithPath: documentDirectory) 
self.localPath = photoURL.appendingPathComponent(imageName!) 

do { 
    try UIImageJPEGRepresentation(image, 1.0)?.write(to: localPath!) 
    print("File Saved") 
    imageArray.append(image) 

    } catch { //Error } 
    self.collectionView.reloadData() 
    } else { //Error here } 
     self.dismiss(animated: true, completion: nil) 
    } 

现在,如果我有2张图片,我想通过他们将我的API调用一个接一个地作为参数。这是我正在做这样的...

for imgURL in imageArray { 
      let url = "http://myapp.com/vw/images_upload" 
      let headers = [ "Content-Type":"application/x-www-form-urlencoded"] 

    let Parameters = 
       [ 
       "image": imgURL, 
       "seller_id": id 
       ] as [String : Any] 

Alamofire.request(url, method: .post, parameters: Parameters, encoding: URLEncoding.httpBody, headers: headers) 

       .responseJSON { (response) in 
        if let httpResponse = response.response { 
         print("error \(httpResponse.statusCode)") 

         if httpResponse.statusCode == 200 { 
          if let result = response.result.value as? [String:Any] { 
           if result["success"] as! Int == 0 { 
            print("Something went wrong!")  
           } else if result["success"] as! Int == 1 { 
            print("UPLOADED IMAGE SUCCESSFULLY!!") 
           }}}}}} 

但在参数时,在imgURL,我没有得到的图像的URL。上面我得到了localPath中的网址。但是我不能通过localPath循环,因为它提供了一个错误。此外,在imageArray中,我传递的图像不是url格式...它的格式如下:<UIImage: 0x60800009f9f0> size {4288, 2848} orientation 0 scale 1.000000... url格式如何传入imageArray,我无法理解。可以这是问题..?

另外我怎样才能得到图像的网址,以便我可以将它传递给API调用...?请帮助...

+0

'让图像信息= [UIImagePickerControllerOriginalImage]为? UIImage'给你一个'UIImage'。然后这个图像通过'imageArray.append(image)'存储在'imageArray'中。如果要将图像的URL存储在数组中,则应该使用'imageArray.append(imageURL)'代替。 –

+0

imageArray是一个UIImage数组,因此它会抛出错误“无法将类型'NSURL'的值转换为期望的参数类型'UIImage' – bws

+0

如果您的'imageArray'是'[UIImage]'的类型,那么您如何期待每个元素作为URL?您必须为图像分别为您的网址创建一个单独的数组。并且您不必首先将图像上传到远程服务器,然后从那里获取URL - >将该URL保存到不同的请求。 – Aks

回答

0

嗨,我在这里从你的问题理解是,你正在设置一个图像到一个ImageView,你想传递该图像的路径到一个API。 所以我写了一个功能,可以根据您的需要来调整大小,以避免或修改。 func storeImage(image:UIImage,fileName:String) - >将图片选择器和默认名称字符串(例如:“image1”)合并,然后返回图像路径 。串{ 变种resizedImage =图像

 func storeImage(image:UIImage, fileName:String) -> String { 
    var resizedImage = image 

    if (image.size.width > 200) { 
     let width = 200.0 as CGFloat 
     let height = image.size.height * width/image.size.width 
     resizedImage = image.scaleImage(toSize: CGSize(width: width, height: height))! 
    } 
    else if (image.size.height > 200) { 
     let height = 200.0 as CGFloat 
     let width = image.size.width * height/image.size.height 
     resizedImage = image.scaleImage(toSize: CGSize(width: width, height: height))! 
    } 

    let imageData = NSData(data:UIImagePNGRepresentation(resizedImage)!) 
    let paths = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true) 
    let docs: String = paths[0] 
    let fullPath = docs.stringByAppendingPathComponent(path: fileName) 

    _ = imageData.write(toFile: fullPath, atomically: true) 
    return fullPath 
}