- (IBAction)chooseImgBtnClick:(id)sender {
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
-(void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
_image=[info objectForKey:UIImagePickerControllerOriginalImage];
[_vehicleImageView setImage:_image];
[self dismissViewControllerAnimated:YES completion:NULL];_vehicleImageView.image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *webData = UIImagePNGRepresentation(_vehicleImageView.image);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:@"png"];
[webData writeToFile:localFilePath atomically:YES];
NSLog(@"localFilePath.%@",localFilePath);
_filePath=localFilePath;
}
影像使用我選擇從庫中的照片,上面的代碼,並設置在UIImageView的照片並試圖採取的路徑到一個名爲localFilePath字符串。我將路徑分配給_filepath。加載圖像到ImageView的,採取從文件路徑
現在我編寫了一個函數,用於從文件路徑中獲取圖像,並在單擊另一個按鈕時將圖像載入另一個UIImageView。 但圖像沒有被加載。請告知
以下是功能。
- (IBAction)loadSecondView:(id)sender {
NSData *pngData = [NSData dataWithContentsOfFile:_filePath];
UIImage *image = [UIImage imageWithData:pngData];
[_secondImageView setImage:image];
}
只有三個sourceTypes可用,其中一個是相機。除非你想要相機膠捲(sourceType == savedPhotosAlbum),否則很難知道你正在尋找什麼,沒有一個更詳細的例子。 iOS設備上的「任何目錄」要麼不明確(你的意思是iCloud還是其他地方的「off-device」?)或不可能的(iOS設備沒有目錄概念)。請給予更多的細節.... – dfd
我同意@dfd。請閱讀這裏的答案: - http://stackoverflow.com/a/1082265/4525734 此外,這是鏈接到蘋果文檔: https://developer.apple.com/reference/uikit/uiimagepickercontrollersourcetype –
是不可能從文檔文件夾中選擇圖像或者在項目中創建的具有一些圖像的任何文件夾? – Harish