Подтвердить что ты не робот

Загрузка изображения из каталога документов в iPhone

Я хочу загрузить изображение в UIImageView из библиотеки документов приложений. Я пытаюсь использовать следующий код, но он не работает.

UIImageView *background = [[[UIImageView alloc] initWithFrame:CGRectMake(3, 10, 48, 36)] autorelease];

[background setImage:[[UIImage imageAtPath:[[NSBundle mainBundle] pathForResource:@"Thumbnail-small" ofType:@"jpg" inDirectory:@"/Users/nbojja/Library/Application Support/iPhone Simulator/User/Applications/60C2E4EC-2FE0-4579-9F86-08CCF078216D/Documents/eb43ac64-8807-4250-8349-4b1f5ddd7d0d/9286371c-564f-40b4-99bd-a2aceb00a6d3/9"]]] retain]];

может кто-то помочь мне с этим. спасибо...

4b9b3361

Ответ 2

Если вы действительно хотите загрузить изображение из папки документов приложений, вы можете использовать это:

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES );
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:@"%@/Thumbnail-small.jpg", docDirectory];
background.image = [[[UIImage alloc] initWithContentsOfFile:filePath] autorelease];

Однако, как отметили другие здесь, вы, вероятно, захотите загрузить его из своего пакета приложений, и в этом случае любой из них будет работать:

background.image = [UIImage imageNamed:@"Thumbnail-small.jpg"];

или

NSString *path = [[NSBundle mainBundle] pathForResource:@"Thumbnail-small" ofType:@"jpg"];
background.image = [UIImage imageWithContentsOfFile:path];

Ответ 3

NSString *docPath = [NSSearchPathForDirectoriesInDomains (NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath=[NSString stringWithFormat:@"%@/image.png",docPath];

BOOL fileExists=[[NSFileManager defaultManager] fileExistsAtPath:filePath];

if (!fileExists)
    NSLog(@"File Not Found");
else
    image = [UIImage imageWithContentsOfFile:filePath];

Ответ 4

Я думаю, что вам действительно нужно:

UIImage *img = [UIImage imageWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"myimagefile" ofType:@"png"]];
[self.testView.imgView setImage:img];

Ответ 5

Swift 3:

let nsDocumentDirectory = FileManager.SearchPathDirectory.documentDirectory
let nsUserDomainMask = FileManager.SearchPathDomainMask.userDomainMask
let paths = NSSearchPathForDirectoriesInDomains(nsDocumentDirectory, nsUserDomainMask, true)
if let dirPath = paths.first {
let imageURL = URL(fileURLWithPath: dirPath).appendingPathComponent("Image2.png")
let image = UIImage(contentsOfFile: imageURL.path)
// Do whatever you want with the image
}

Ответ 6

Используйте [UIImage imageNamed:@"ThumbnailSmall.jpg"];

Это будет загружать то, что вы хотите, я думаю.