iPhone 使用唯一键加载和保存多个 UIImage?

发布于 2024-12-16 23:49:47 字数 2354 浏览 1 评论 0原文

我知道有一些解决方案可以在 iPhone 上保存和加载图像。然而,没有一个对我很有用。

例如,下面的链接有一个很棒的代码块,它可以让您使用唯一的键加载和保存 UIImage,但我无法弄清楚如何在使用此代码加载图像时判断该键下的图像是否已存在或不是。

http://www.Friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/

//saving an image
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
     NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
     NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
     NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
     [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
     NSLog(@"image saved");
}

//removing an image
- (void)removeImage:(NSString*)fileName {
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
     [fileManager removeItemAtPath: fullPath error:NULL];
     NSLog(@"image removed");
}

//loading an image
- (UIImage*)loadImage:(NSString*)imageName {
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
     return [UIImage imageWithContentsOfFile:fullPath];
}

如果您能帮我解决这个问题,我将不胜感激。我需要保存多个图像,并根据给定的密钥是否已存在来加载它们。

I know there are some solutions out there for saving and loading images on your iPhone. However none have quite worked for me.

For example, the link below has a great code chunk which lets you load and save a UIImage with a unique key, but I can't work out how to tell when loading an image using this code if an image under that key already exists or not.

http://www.friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/

//saving an image
- (void)saveImage:(UIImage*)image:(NSString*)imageName {
     NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format.
     NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it
     NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path
     [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image)
     NSLog(@"image saved");
}

//removing an image
- (void)removeImage:(NSString*)fileName {
     NSFileManager *fileManager = [NSFileManager defaultManager];
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", fileName]];
     [fileManager removeItemAtPath: fullPath error:NULL];
     NSLog(@"image removed");
}

//loading an image
- (UIImage*)loadImage:(NSString*)imageName {
     NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
     NSString *documentsDirectory = [paths objectAtIndex:0];
     NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]];
     return [UIImage imageWithContentsOfFile:fullPath];
}

If you could help me work out that out it would be really appreciated. I need to save several images, and load them depending on if a given key already exists or not.

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

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

发布评论

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

评论(2

一身骄傲 2024-12-23 23:49:47

如果你想获取具有唯一密钥的图像,请尝试使用 UUID。这将始终为您提供具有唯一密钥的图像。例如,这里是使用 UUID 的示例。

- (NSString *)pathForImageFileWithPrefix:(NSString *)Prefix
{
    CFUUIDRef   uuid;
    CFStringRef uuidStr;
    uuid = CFUUIDCreate(NULL);
    uuidStr = CFUUIDCreateString(NULL, uuid);
    NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);   
    NSString* documentsDirectory = [paths objectAtIndex:0]; 
    result = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.png",Prefix, uuidStr]];
    [imageData writeToFile:result atomically:NO];
    CFRelease(uuidStr);
    CFRelease(uuid);
    return result;
}

if u want to get images with unique key, try using UUID. This will always give u image with unique key..For example here is an example of using UUID..

- (NSString *)pathForImageFileWithPrefix:(NSString *)Prefix
{
    CFUUIDRef   uuid;
    CFStringRef uuidStr;
    uuid = CFUUIDCreate(NULL);
    uuidStr = CFUUIDCreateString(NULL, uuid);
    NSData *imageData = UIImageJPEGRepresentation(chosenImage, 1.0);
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);   
    NSString* documentsDirectory = [paths objectAtIndex:0]; 
    result = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%@.png",Prefix, uuidStr]];
    [imageData writeToFile:result atomically:NO];
    CFRelease(uuidStr);
    CFRelease(uuid);
    return result;
}
〃温暖了心ぐ 2024-12-23 23:49:47

如果我回答正确,您想知道具有给定图像名称的文件是否已经存在?然后你可以使用

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

If I get your question right, you want to know whether a file with the given image name already exists? Then you can just use

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

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