[英]Loading a photo taken from UIImagePickerController camera asynchronously
[英]How to upload image that was taken from UIImagePickerController
用戶使用UIImagePickerController
從iPhone庫中選擇圖像后,我想使用ASIHTTPRequest
庫將其上傳到我的服務器。
我知道使用ASIHTTPRequest
我可以使用文件的URl上傳文件,但是如何獲取圖像URL?
我知道我可以得到圖像UIImagePickerControllerReferenceURL
,看起來像這樣:
"assets-library://asset/asset.JPG?id=F2829B2E-6C6B-4569-932E-7DB03FBF7763&ext=JPG"
這是我需要使用的URL嗎?
有兩種方法
1:
您可以使用imagePickerController委托上傳圖像
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
//Upload your image
}
2:
您可以保存已挑選的圖像網址,然后使用此功能上傳
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *imageUrl = [NSString stringWithFormat:@"%@",[info valueForKey:UIImagePickerControllerReferenceURL]];
//Save the imageUrl
}
-(void)UploadTheImage:(NSString *)imageUrl{
NSURL *url = [[NSURL alloc] initWithString:imageUrl];
typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
ALAssetRepresentation *rep = [myasset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *myImage = nil;
if (ref) {
myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
//upload the image
}
};
ALAssetsLibraryAccessFailureBlock failureblock = ^(NSError *myerror){
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:url resultBlock:result block failureBlock:failureblock];
}
注意:在使用ARC.Better將ALAssetsLibrary
對象用作單例時,請確保ALAssetsLibrary
對象的范圍。
將照片保存在文檔目錄中並使用該URL上傳。例如
NSString *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[UIImageJPEGRepresentation(img, 1.0) writeToFile:jpgPath atomically:YES];
將此圖像上傳為[request setFile:jpgPath forKey:@"image"]
;
有許多答案表明UIImageJPEGRepresentation或UIImagePNGRepresentation。 但是這些解決方案將轉換原始文件,而這個問題實際上是關於按原樣保存文件。
看起來直接從資產庫上傳文件是不可能的。 但是可以使用PHImageManager訪問它以獲取實際的圖像數據。 這是如何做:
Swift 3 (Xcode 8,僅適用於iOS 8.0及更高版本)
1)導入Photos框架
import Photos
2)在imagePickerController(_:didFinishPickingMediaWithInfo :)中獲取資產URL
3)使用fetchAssets獲取資產(withALAssetURLs:options :)
4)使用requestImageData獲取實際圖像數據(for:options:resultHandler :)。 在此方法的結果處理程序中,您將獲得數據以及文件的URL(可以在模擬器上訪問URL,但遺憾的是在設備上無法訪問 - 在我的測試中,startAccessingSecurityScopedResource()始終返回false)。 但是,此URL仍可用於查找文件名。
示例代碼:
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {
dismiss(animated: true, completion: nil)
if let assetURL = info[UIImagePickerControllerReferenceURL] as? URL,
let asset = PHAsset.fetchAssets(withALAssetURLs: [assetURL], options: nil).firstObject,
let documentsPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first,
let targetURL = Foundation.URL(string: "file://\(documentsPath)") {
PHImageManager.default().requestImageData(for: asset, options: nil, resultHandler: { (data, UTI, _, info) in
if let imageURL = info?["PHImageFileURLKey"] as? URL,
imageData = data {
do {
try data.write(to: targetURL.appendingPathComponent(imageURL.lastPathComponent), options: .atomic)
self.proceedWithUploadFromPath(targetPath: targetURL.appendingPathComponent(imageURL.lastPathComponent))
} catch { print(error) }
}
}
})
}
}
這將為您提供AS IS文件,包括其正確的名稱,您甚至可以讓UTI確定正確的mimetype(除了通過文件擴展名確定),同時准備上傳多部分正文。
獲取圖像UIImagePickerControllerReferenceUR。 該示例代碼如下
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
{
ALAssetRepresentation *representation = [myasset defaultRepresentation];
NSString *fileName = [representation filename];
NSLog(@"fileName : %@",fileName);
CGImageRef ref = [representation fullResolutionImage];
ALAssetOrientation orientation = [[myasset valueForProperty:@"ALAssetPropertyOrientation"] intValue];
UIImage *image = [UIImage imageWithCGImage:ref scale:1.0 orientation:(UIImageOrientation)orientation];
};
ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
[assetslibrary assetForURL:imageURL
resultBlock:resultblock
failureBlock:nil];
}
注意:它僅適用於iOS 5及更高版本。
我找到的最簡單的方法是
第1步:獲取DocumentsDirectory的路徑
func fileInDocumentsDirectory(filename: String) -> String {
let documentsFolderPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true)[0] as NSString
return documentsFolderPath.appendingPathComponent(filename)
}
第2步:使用tempFileName保存路徑
func saveImage(image: UIImage, path: String ) {
let pngImageData = UIImagePNGRepresentation(image)
do {
try pngImageData?.write(to: URL(fileURLWithPath: path), options: .atomic)
} catch {
print(error)
}
}
第3步:在imagePickerController函數中使用
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]){
if var image = info[UIImagePickerControllerOriginalImage] as? UIImage {// image asset
self.saveImage(image: image, path: fileInDocumentsDirectory(filename: "temp_dummy_image.png"))
}
第4步:在需要時獲取圖像
得到像這樣的參考
let localfilepath = self.fileInDocumentsDirectory(filename:“temp_dummy_image.png”)
步驟5:使用圖像后,刪除臨時圖像
func removeTempDummyImagefileInDocumentsDirectory(filename: String) {
let fileManager = FileManager.default
let documentsUrl = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first! as NSURL
let documentsPath = documentsUrl.path
do {
if let documentPath = documentsPath
{
let fileNames = try fileManager.contentsOfDirectory(atPath: "\(documentPath)")
print("all files in cache: \(fileNames)")
for fileName in fileNames {
if (fileName.hasSuffix(".png"))
{
let filePathName = "\(documentPath)/\(fileName)"
try fileManager.removeItem(atPath: filePathName)
}
}
let files = try fileManager.contentsOfDirectory(atPath: "\(documentPath)")
print("all files in cache after deleting images: \(files)")
}
} catch {
print("Could not clear temp folder: \(error)")
}
}
您可以通過創建表單上傳圖像嘗試下面的代碼
-(void)UploadImage{
NSString *urlString = @"yourUrl";
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];
// file
NSData *imageData = UIImageJPEGRepresentation([self scaleAndRotateImage:[selectedImageObj]],90);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[[NSString stringWithString:@"Content-Disposition: attachment; name=\"user_photo\"; filename=\"photoes.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\"; filename=\"%@.jpg\"\r\n",@"ImageNmae"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithString:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// set request body
[request setHTTPBody:body];
//return and test
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.