[英]Delete image in app document directory
I have 24 images in a "PhotoAlbum" folder in my app document directory. 我的app文档目录中的“PhotoAlbum”文件夹中有24个图像。 How to delete a selected image in the "PhotoAlbum"? 如何删除“PhotoAlbum”中的所选图像? How do I pass the object index? 如何传递对象索引?
I have something like this: 我有这样的事情:
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/PhotoAlbum"];
NSString *fullPath = [dataPath stringByAppendingPathComponent:[self.photos objectAtIndex:index]];
NSData *pngData = [NSData dataWithContentsOfFile:fullPath];
UIImage *image = [UIImage imageWithData:pngData];
MWPhoto *photo = [[MWPhoto alloc] initWithImage:image];
return photo;
}
You can't delete it by specifying an index. 您无法通过指定索引来删除它。 You have to specify the image name you wanted to delete. 您必须指定要删除的图像名称。 It will be something like this 它会是这样的
NSString *filePath = [NSString stringWithFormat:@"%@/%@",documentPath,image.imageName];
NSError *error = nil;
if ([[NSFileManager defaultManager] fileExistsAtPath:documentPath]){
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
This will delete your image and return deleted image. 这将删除您的图像并返回已删除的图像。
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/PhotoAlbum"];
NSString *fullPath = [dataPath stringByAppendingPathComponent:[self.photos objectAtIndex:index]];
NSData *pngData = [NSData dataWithContentsOfFile:fullPath];
UIImage *image = [UIImage imageWithData:pngData];
MWPhoto *photo = [[MWPhoto alloc] initWithImage:image];
if ([[NSFileManager defaultManager] fileExistsAtPath:fullPath]){
[[NSFileManager defaultManager] removeItemAtPath:fullPath error:&error];
}
return photo;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.