无法从 iPhone 设备文件夹获取图像

发布于 2024-11-26 09:48:02 字数 1246 浏览 2 评论 0原文

我正在尝试从设备(iPhone)获取一些图像并将其显示在屏幕上。

图像文件保存在我的设备上,并分隔在如下文件夹中:(...)/Documents/1/image1.png。

这是我尝试获取图像的函数的代码

-(UIImage *) getImageFromDevice:(NSString *) fileName 
                        idImage:(int)        idImage{

    NSString *path;

    path = [[self dataFilePath] stringByAppendingString:@"/"];
    path = [path stringByAppendingString: [[NSString alloc] initWithFormat:@"%d",idImage]];
    path = [path stringByAppendingString:@"/"];
    path = [path stringByAppendingString:fileName];

    NSLog(@"path = %@", path);

    NSURL *deviceImageUrl = [[NSURL alloc] initWithString:path];
    NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
    UIImage *deviceImage = [UIImage imageWithData:imageData];

    return deviceImage;

}

,这是获取工作正常的设备文件夹路径的函数:

-(NSString *)dataFilePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}

但是当我使用<设置NSURL *deviceImageUrl时strong>[[NSURL alloc] initWithString:path] deviceImageUrl 变为nil

我做错了什么?

I'm trying to get some images from the device (iPhone) and show them on screen.

The image files are saved on my device and separated in folders like this: (...)/Documents/1/image1.png.

Here is the code of the function where I'm trying to get the image

-(UIImage *) getImageFromDevice:(NSString *) fileName 
                        idImage:(int)        idImage{

    NSString *path;

    path = [[self dataFilePath] stringByAppendingString:@"/"];
    path = [path stringByAppendingString: [[NSString alloc] initWithFormat:@"%d",idImage]];
    path = [path stringByAppendingString:@"/"];
    path = [path stringByAppendingString:fileName];

    NSLog(@"path = %@", path);

    NSURL *deviceImageUrl = [[NSURL alloc] initWithString:path];
    NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
    UIImage *deviceImage = [UIImage imageWithData:imageData];

    return deviceImage;

}

And this is the function to get the path to the device folder that's working fine:

-(NSString *)dataFilePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];

    return documentsDirectory;

}

But when I set the NSURL *deviceImageUrl with [[NSURL alloc] initWithString:path] the deviceImageUrl becomes nil.

What I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

夏夜暖风 2024-12-03 09:48:02
//NSURL *deviceImageUrl = [[NSURL alloc] initWithString:path];
//NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
//UIImage *deviceImage = [UIImage imageWithData:imageData];    
UIImage *deviceImage = [[[UIImage alloc] initWithContentsOfFile:path]autorelease];
//NSURL *deviceImageUrl = [[NSURL alloc] initWithString:path];
//NSData *imageData = [NSData dataWithContentsOfURL:deviceImageUrl];
//UIImage *deviceImage = [UIImage imageWithData:imageData];    
UIImage *deviceImage = [[[UIImage alloc] initWithContentsOfFile:path]autorelease];
夏见 2024-12-03 09:48:02

尝试使用 fileURLWithPath: 而不是 URLWithString: ;)

NSURL *deviceImageUrl = [NSURL fileURLWithPath:path]; // autoreleased url

我也总是搞乱这两种方法......它们让我发疯。

Try using fileURLWithPath: instead of URLWithString: ;)

NSURL *deviceImageUrl = [NSURL fileURLWithPath:path]; // autoreleased url

I always mess up with these two methods, too ... they're making me crazy.

苯莒 2024-12-03 09:48:02

您不应使用 initWithString: ,这必须符合 RFC 中描述的 URL 格式。如果你只想使用系统路径,请使用这个:(使用后不要忘记释放)

NSURL *deviceImageUrl = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];

You should not use initWithString: , this must conform to URL format as described in RFCs. If you just want to use a system path, use this : (don't forget to release after use it)

NSURL *deviceImageUrl = [[NSURL alloc] initFileURLWithPath:path isDirectory:NO];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文