2016-02-28 1 views
0

Je crée une application qui capture d'écran du bureau mac ...répertoire de bureau Xcode et writeToFile: toujours avec un autre nom CACAO APPLICATION

Mais j'ai 2 problèmes:

ce qui est le répertoire « global » de bureau?

Dans mon ordinateur est /Users/miguelcosta/Desktop/, mais je veux un répertoire qui fonctionne pour tous les macs ..

Mon deuxième problème est le suivant:

Quand il crée Capture d'écran d'une image sur votre bureau avec le nom Result.jpg . Cependant quand vous prenez une capture d'écran, il remplace la capture d'écran précédente ... Je pensais comment puis-je enregistrer ces images toujours avec un nom différent ...

Voici mon code:

NSString * targetPath = @ "/Users/miguelcosta/Desktop/Result.jpg";

NSData *imageData = [newImage TIFFRepresentation]; 
NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData]; 
NSDictionary *imageProps = [NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.9] forKey:NSImageCompressionFactor]; 
imageData = [imageRep representationUsingType:NSJPEGFileType properties:imageProps]; 
[imageData writeToFile:targetPath atomically:NO]; 

Merci pour votre aide!

+0

Que voulez-vous dire par un répertoire qui fonctionne pour tous les macs? Vous voulez dire, pour tous les utilisateurs? –

Répondre

0

Voici un extrait de code ... Reportez-vous également à ce sujet: Getting desktop path for current user on OS X

#import "ViewController.h" 

@interface ViewController() 
{ 
    NSInteger _count; 
} 

@end 

@implementation ViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    _count = 1; 

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

    NSString *fileName = @"Result.jpg"; 
    NSString *filePath = [NSString stringWithFormat:@"%@/%@", desktopPath, fileName]; 

    filePath = [self filePathWithPath:filePath]; 

    // write to file now using filePath ... 

} 

// generate new name ... 
- (NSString*)filePathWithPath:(NSString*)filePath 
{ 
    if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) { 
     NSArray *parts = [[filePath lastPathComponent] componentsSeparatedByString:@"."]; 
     NSString *name = parts[0]; 
     NSString *ext = parts[1]; 
     filePath = [self filePathWithPath:[NSString stringWithFormat:@"%@/%@%ld.%@", filePath, name, _count++, ext]]; 
    } 

    return filePath; 
} 


@end 
+0

Au lieu de jongler avec "/" et "." utilisez les propriétés de chemin et les méthodes de NSString comme 'lastPathComponent' et' pathExtension' ou utilisez NSURL. – Willeke

+0

Modifié: 'NSArray * parts = [[filePath lastPathComponent] componentsSeparatedByString: @". "];' – schmidt9