[英]Display Image using asset path or local file path
我正在嘗試使用swift顯示相機膠卷中的圖像表。 我有資產路徑:
assets-library://asset/asset.JPG?id=123456789&ext=JPG
和本地文件路徑:
/Users/johndoe/Library/Developer/CoreSimulator/Devices/1932149090125/data/Containers/Data/Application/11451-54365436-34563-2345-232342364/Documents/asset.JPG
我不知道使用什么函數來創建UIImage。
我嘗試了UIImage(data: NSdata)
和UIImage(contentsOfFile: String)
沒有成功。
代碼 :在UITableView的cellForRowAtIndexPath函數中,我正在遍歷具有單元的資產路徑和本地文件路徑的數據庫。
let url = NSURL(string: "/Users/johndoe/Library/Developer/CoreSimulator/Devices/123455678/data/Containers/Data/Application/124536432342364/Documents/asset.JPG")
let data = NSData(contentsOfURL: url!)
cell.thumbnailImage.image = UIImage(data: data!)
我正在嘗試使用swift顯示相機膠卷中的圖像表
好吧,這是一項復雜的工作。 您必須使用PhotoKit。 將照片獲取為PHAsset,然后調用PHImageManager.defaultManager().requestImageForAsset
以獲取實際圖像。 圖像異步到達,並且隨着圖像質量的提高可能多次到達,因此您必須為此做好准備。
有一個函數,當在imagePickerController中選擇了圖片時會調用該函數,請參見下文。 確保您的ViewController使用UIImagePickerControllerDelegate協議。
然后在我的.m文件中,我具有屬於該協議的功能:
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {//在拍攝或拍攝每張照片后來到這里
UIImage *img = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
等,等等}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.