Supongamos que tiene una imagen temporal llamada "imageTexture.jpg" guardada en el directorio de caché. El favorito "FavoritePhoto.jpg" se guarda en el directorio de documentos. Para sobrescribir el favorito en el directorio de documentos, puede hacer esto.
NSError *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *statesDescriptionPath = [documentsDirectory stringByAppendingPathComponent:@"FavoritePhoto.jpg"];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *cacheDirectory = [NSFileManager getCacheDirectory];
NSString *temporaryPath = [cacheDirectory stringByAppendingPathComponent:@"imageTexture.jpg"];
NSURL *originalURL = [NSURL fileURLWithPath:statesDescriptionPath];
[fileManager replaceItemAtURL:originalURL withItemAtURL:[NSURL fileURLWithPath:temporaryPath] backupItemName:nil options:NSFileManagerItemReplacementUsingNewMetadataOnly resultingItemURL:&originalURL error:&errorDesc];
if (errorDesc)
{
NSLog(@"there was an error overwriting the favorite photo: %@", errorDesc.description);
}
estoy usando una categoría NSFileManager para obtener el directorio de caché
Este es el código para NSFileManager + Powertools.h
#import <Foundation/Foundation.h>
@interface NSFileManager (Powertools)
+ (NSString *)getCacheDirectory;
@end
Aquí se puede ver el código de NSFileManager + Powertools. m
#import "NSFileManager+Powertools.h"
@implementation NSFileManager (Powertools)
+ (NSString *)getCacheDirectory
{
NSString *path = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
if ([paths count])
{
NSString *bundleName =
[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleIdentifier"];
path = [[paths objectAtIndex:0] stringByAppendingPathComponent:bundleName];
}
return path;
}
@end
se proporciona una buena solución en otro post aquí: http://stackoverflow.com/questions/9876157/is -the-current-location-compass-heading-button-available-in-the-ios-sdk –