I am creating a application that screenshot the mac desktop...
But I am having 2 problems:
what is the "global" directory of desktop?
In my computer is /Users/miguelcosta/Desktop/
but i want a directory that works for all macs..
My second problem is:
When you screenshot he creates a image on your desktop with the name Result.jpg. However when you take another screenshot he replace the previous screenshot... So I was thinking how can I save this images always with a different name...
Here is my 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];
Thanks for your help!
Here is a code snippet... Refer also to this topic: 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.