Skip to content

Commit d5e7f8b

Browse files
committed
Added support for macOS platform
1 parent 11ab4a5 commit d5e7f8b

File tree

5 files changed

+562
-1
lines changed

5 files changed

+562
-1
lines changed

LSFileWrapper.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,18 @@
55
//
66

77
#import <Foundation/Foundation.h>
8+
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
89
#import <AssetsLibrary/AssetsLibrary.h>
10+
#import <UIKit/UIKit.h>
11+
#else
12+
#import <AppKit/AppKit.h>
13+
#endif
14+
15+
//! Project version number for LSFileWrapper.
16+
FOUNDATION_EXPORT double LSFileWrapperVersionNumber;
17+
18+
//! Project version string for LSFileWrapper.
19+
FOUNDATION_EXPORT const unsigned char LSFileWrapperVersionString[];
920

1021
@interface LSFileWrapper : NSObject
1122

@@ -16,7 +27,11 @@
1627
- (NSData *)data;
1728
- (NSString *)string;
1829
- (NSDictionary *)dictionary;
30+
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
1931
- (UIImage *)image;
32+
#else
33+
- (NSImage *)image;
34+
#endif
2035

2136
- (void)updateContent:(id<NSObject>)content;
2237
- (void)deleteContent;

LSFileWrapper.m

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ - (NSDictionary *)dictionary
136136
return nil;
137137
}
138138

139+
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
139140
- (UIImage *)image
140141
{
141142
if (content == nil && writtenURL != nil) {
@@ -151,6 +152,22 @@ - (UIImage *)image
151152
}
152153
return nil;
153154
}
155+
#else
156+
- (NSImage *)image {
157+
if (content == nil && writtenURL != nil) {
158+
content = [[NSImage alloc] initWithContentsOfFile:writtenURL.path];
159+
cacheFile = YES;
160+
return (NSImage *)content;
161+
}
162+
if ([content isKindOfClass:[NSData class]]) {
163+
content = [[NSImage alloc] initWithData:(NSData *)content];
164+
}
165+
if ([content isKindOfClass:[NSImage class]]) {
166+
return (NSImage *)content;
167+
}
168+
return nil;
169+
}
170+
#endif
154171

155172
- (void)updateContent:(id<NSObject>)content_
156173
{
@@ -372,6 +389,7 @@ - (NSString *)setFileWrapper:(LSFileWrapper *)fileWrapper filename:(NSString *)f
372389
return filename_;
373390
}
374391

392+
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
375393
- (BOOL)writeAsset:(ALAsset *)asset_ toURL:(NSURL *)url fileManager:(NSFileManager *)fileManager error:(NSError *__autoreleasing *)outError
376394
{
377395
[fileManager createFileAtPath:[url path] contents:nil attributes:nil];
@@ -382,7 +400,7 @@ - (BOOL)writeAsset:(ALAsset *)asset_ toURL:(NSURL *)url fileManager:(NSFileManag
382400
return NO;
383401
}
384402
ALAssetRepresentation *rep = [asset_ defaultRepresentation];
385-
NSUInteger bytesSize = [rep size];
403+
NSUInteger bytesSize = (NSUInteger)[rep size];
386404
NSUInteger bytesRead = 0;
387405
NSUInteger bytesOffset = 0;
388406
uint8_t *buffer = calloc(BufferSize, sizeof(*buffer));
@@ -422,6 +440,27 @@ - (BOOL)writeImage:(UIImage *)image_ toURL:(NSURL *)url error:(NSError *__autore
422440
return [imageData writeToURL:url options:NSDataWritingAtomic error:outError];
423441
}
424442

443+
#else
444+
- (BOOL)writeImage:(NSImage *)image_ toURL:(NSURL *)url error:(NSError *__autoreleasing *)outError
445+
{
446+
NSData *imageData;
447+
NSString *extension = [url pathExtension];
448+
449+
[image_ lockFocus];
450+
NSBitmapImageRep *bitmapRep = [[NSBitmapImageRep alloc] initWithFocusedViewRect:NSMakeRect(0, 0, image_.size.width, image_.size.height)];
451+
[image_ unlockFocus];
452+
453+
if ([extension isEqualToString:@"png"]) {
454+
imageData = [bitmapRep representationUsingType:NSPNGFileType properties:nil];
455+
}
456+
else if ([extension isEqualToString:@"jpg"] || [extension isEqualToString:@"jpeg"]) {
457+
imageData = [bitmapRep representationUsingType:NSJPEGFileType properties:nil];
458+
}
459+
460+
return [imageData writeToURL:url options:NSDataWritingAtomic error:outError];
461+
}
462+
#endif
463+
425464
- (BOOL)getParentUpdates:(NSMutableArray *)updates withURL:(NSURL *)url
426465
{
427466
if (!parent) {
@@ -490,12 +529,18 @@ - (BOOL)writeUpdates:(NSArray *)updates filemanager:(NSFileManager *)fileManager
490529
else if ([fileContent isKindOfClass:[NSString class]]) {
491530
success = [(NSString*)fileContent writeToURL:fileURL atomically:YES encoding:NSUTF8StringEncoding error:outError];
492531
}
532+
#if TARGET_OS_IPHONE || TARGET_OS_SIMULATOR
493533
else if ([fileContent isKindOfClass:[UIImage class]]) {
494534
success = [self writeImage:(UIImage *)fileContent toURL:fileURL error:outError];
495535
}
496536
else if ([fileContent isKindOfClass:[ALAsset class]]) {
497537
success = [self writeAsset:(ALAsset *)fileContent toURL:fileURL fileManager:fileManager error:outError];
498538
}
539+
#else
540+
else if ([fileContent isKindOfClass:[NSImage class]]) {
541+
success = [self writeImage:(NSImage *)fileContent toURL:fileURL error:outError];
542+
}
543+
#endif
499544
else {
500545
NSData *data = [NSPropertyListSerialization dataWithPropertyList:fileContent
501546
format:NSPropertyListBinaryFormat_v1_0

0 commit comments

Comments
 (0)