从UIImagePickerControllerReferenceURL访问ext

时间:2012-06-28 13:05:56

标签: ios

如何只访问UIImagePickerControllerReferenceURL的MIME ext部分?这是我在NSLog时得到的 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

UIImagePickerControllerReferenceURL = "assets-library://asset/asset.PNG?id=F61B754A-705C-4BFB-9965-DE34C7B93A2B&ext=PNG";

我只想访问MIME类型。当我使用以下方式将图像发送到我的网络服务器时

NSData *imageData = UIImageJPEGRepresentation(_selectedImage, 1.0);

//non-important ASIFormDataRequest set-up
[request setData:imageData withFileName:uniqueString andContentType:@"image/jpeg" forKey:@"photo"];

图片全部搞砸了。但是当我使用:

NSData *imageData = UIImagePNGRepresentation(_selectedImage);

//non-important ASIFormDataRequest set-up
[request setData:imageData withFileName:uniqueString andContentType:@"image/png" forKey:@"photo"];

图片很好。但我上传的所有照片都不会是PNG。

3 个答案:

答案 0 :(得分:1)

Dimitris对此thread的回答就像一个魅力。我现在要继续打字,所以这不会降级为一个简单的答案,然后自动发表评论。

答案 1 :(得分:1)

要在swift中获取图片扩展名:

 func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
            if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {
                let assetPath = info[UIImagePickerControllerReferenceURL] as! NSURL
                let ext = assetPath.absoluteString.componentsSeparatedByString("ext=")[1]
                 print(ext)
        }
    }

答案 2 :(得分:0)

请尝试以下代码从extension

中提取UIImagePickerControllerReferenceURL
NSURL *referenceURL = @"assets-library://asset/asset.PNG?id=F61B754A-705C-4BFB-9965-DE34C7B93A2B&ext=PNG"; //put your reference URL here
NSString *fileExtension = [referenceURL.absoluteString componentsSeparatedByString:@"ext="][1];