Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
//
// SYMetadata.h
// SYPictureMetadataExample
//
// Created by Stan Chevallier on 12/13/12.
// Copyright (c) 2012 Syan. All rights reserved.
//

#import <Foundation/Foundation.h>
#import <Photos/Photos.h> // Using Photos framework
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce header coupling and speed up builds, prefer forward-declaring PHAsset in the header and keep the Photos import in the .m file. For example, replace the import with '@Class PHAsset;' here (Photos is already imported in SYMetadata.m).

Suggested change
#import <Photos/Photos.h> // Using Photos framework
@class PHAsset; // Forward declaration to reduce header coupling

Copilot uses AI. Check for mistakes.

#import "SYMetadataTIFF.h"
#import "SYMetadataGIF.h"
#import "SYMetadataJFIF.h"
Expand All @@ -26,11 +24,6 @@
#import "SYMetadataDNG.h"
#import "SYMetadataExifAux.h"

// http://www.sno.phy.queensu.ca/~phil/exiftool/TagNames/index.html
// http://www.exiv2.org/tags.html

@class ALAsset;

@interface SYMetadata : SYMetadataBase

@property SYMETADATA_PROPERTY_COPY NSDictionary *originalDictionary;
Expand All @@ -54,7 +47,7 @@
@property SYMETADATA_PROPERTY_STRONG SYMetadataDNG *metadataDNG;
@property SYMETADATA_PROPERTY_STRONG SYMetadataExifAux *metadataExifAux;

// we don't know how to parse those, so we juste give access to them
// Fallback
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment 'Fallback' is vague compared to the original explanation. Consider clarifying intent (e.g., 'Unparsed vendor-specific dictionaries exposed as-is for consumers').

Suggested change
// Fallback
// Unparsed vendor-specific dictionaries exposed as-is for consumers (fallback for unknown or proprietary metadata)

Copilot uses AI. Check for mistakes.
@property SYMETADATA_PROPERTY_COPY NSDictionary *metadataApple;
@property SYMETADATA_PROPERTY_COPY NSDictionary *metadataPictureStyle;

Expand All @@ -72,12 +65,11 @@
@property (nonatomic, copy, readonly) NSString *profileName;

+ (instancetype)metadataWithDictionary:(NSDictionary *)dictionary;
+ (instancetype)metadataWithAsset:(ALAsset *)asset __TVOS_PROHIBITED;
+ (instancetype)metadataWithAssetURL:(NSURL *)assetURL __TVOS_PROHIBITED;
+ (instancetype)metadataWithPHAsset:(PHAsset *)asset; // Using ALAsset
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This renames/removes previously public APIs (+metadataWithAsset: and +dictionaryWithAssetURL:), creating a breaking change contrary to the PR description. To preserve source compatibility, consider reintroducing a deprecated wrapper for +dictionaryWithAssetURL: that fetches a PHAsset via Photos (e.g., using [PHAsset fetchAssetsWithALAssetURLs:options:]) and forwards to +dictionaryWithPHAsset:. If you intentionally drop ALAsset support, please note it clearly as a breaking change.

Copilot uses AI. Check for mistakes.
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inline comments are incorrect: these methods use PHAsset/Photos, not ALAsset/ALAssetsLibrary. Please update the comments to avoid confusion (e.g., 'PHAsset-based' or 'Using Photos framework').

Copilot uses AI. Check for mistakes.
+ (instancetype)metadataWithFileURL:(NSURL *)fileURL;
+ (instancetype)metadataWithImageData:(NSData *)imageData;

+ (NSDictionary *)dictionaryWithAssetURL:(NSURL *)assetURL __TVOS_PROHIBITED;
+ (NSDictionary *)dictionaryWithPHAsset:(PHAsset *)asset; // Using ALAssetsLibrary
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The inline comments are incorrect: these methods use PHAsset/Photos, not ALAsset/ALAssetsLibrary. Please update the comments to avoid confusion (e.g., 'PHAsset-based' or 'Using Photos framework').

Copilot uses AI. Check for mistakes.

Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This renames/removes previously public APIs (+metadataWithAsset: and +dictionaryWithAssetURL:), creating a breaking change contrary to the PR description. To preserve source compatibility, consider reintroducing a deprecated wrapper for +dictionaryWithAssetURL: that fetches a PHAsset via Photos (e.g., using [PHAsset fetchAssetsWithALAssetURLs:options:]) and forwards to +dictionaryWithPHAsset:. If you intentionally drop ALAsset support, please note it clearly as a breaking change.

Suggested change
// Deprecated: Use dictionaryWithPHAsset: instead. This wrapper fetches PHAsset from ALAsset URL.
+ (NSDictionary *)dictionaryWithAssetURL:(NSURL *)assetURL __attribute__((deprecated("Use dictionaryWithPHAsset: instead.")));

Copilot uses AI. Check for mistakes.
+ (NSData *)dataWithImageData:(NSData *)imageData andMetadata:(SYMetadata *)metadata;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
// SYPictureMetadataExample
//
// Created by Stan Chevallier on 12/13/12.
// Copyright (c) 2012 Syan. All rights reserved.
// Updated by Alfin (2025) – Migrated to PHAsset / Photos framework
//

#import <ImageIO/ImageIO.h>
#import "SYMetadata.h"
#import "NSDictionary+SY.h"

#if !TARGET_OS_TV
#import <AssetsLibrary/AssetsLibrary.h>
#endif
#import <Photos/Photos.h>

#define SYKeyForMetadata(name) NSStringFromSelector(@selector(metadata##name))
#define SYDictionaryForMetadata(name) SYPaste(SYPaste(kCGImageProperty,name),Dictionary)
Expand All @@ -32,79 +29,96 @@ + (instancetype)metadataWithDictionary:(NSDictionary *)dictionary
{
if (!dictionary)
return nil;

NSError *error;

SYMetadata *instance = [MTLJSONAdapter modelOfClass:self.class fromJSONDictionary:dictionary error:&error];

if (instance)
instance->_originalDictionary = dictionary;

if (error)
NSLog(@"--> Error creating %@ object: %@", NSStringFromClass(self.class), error);

return instance;
}

+ (instancetype)metadataWithAsset:(ALAsset *)asset
+ (instancetype)metadataWithPHAsset:(PHAsset *)asset
{
#if !TARGET_OS_TV
ALAssetRepresentation *representation = [asset defaultRepresentation];
return [self metadataWithDictionary:[representation metadata]];
#else
return nil;
#endif
}
if (!asset) return nil;

+ (instancetype)metadataWithAssetURL:(NSURL *)assetURL
{
NSDictionary *dictionary = [self dictionaryWithAssetURL:assetURL];
return [self metadataWithDictionary:dictionary];
__block SYMetadata *result = nil;

PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.synchronous = YES;
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When assets are iCloud-only, requestImageDataAndOrientationForAsset: will return nil unless network access is allowed. Set 'options.networkAccessAllowed = YES' (and optionally inspect 'info[PHImageErrorKey]' / 'info[PHImageCancelledKey]') to reliably obtain metadata for iCloud-backed assets.

Suggested change
options.synchronous = YES;
options.synchronous = YES;
options.networkAccessAllowed = YES;

Copilot uses AI. Check for mistakes.
Comment on lines +52 to +55
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using 'options.synchronous = YES' will block the calling thread until the result is ready, which can cause UI jank if invoked on the main thread. Consider an async variant (completion-based API) or enforce off-main-thread usage and document this constraint.

Copilot uses AI. Check for mistakes.
Comment on lines +52 to +55
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PHAsset-to-metadata extraction logic is duplicated in both '+metadataWithPHAsset:' and '+dictionaryWithPHAsset:'. Consider extracting a shared helper (e.g., a static method that returns the metadata dictionary) and have both methods call it to reduce duplication and keep behavior consistent.

Copilot uses AI. Check for mistakes.

[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:asset
options:options
resultHandler:^(NSData * _Nullable imageData,
NSString * _Nullable dataUTI,
CGImagePropertyOrientation orientation,
NSDictionary * _Nullable info) {
if (imageData) {
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
if (source) {
NSDictionary *metadataDict = (__bridge_transfer NSDictionary *)
CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
if (metadataDict) {
result = [SYMetadata metadataWithDictionary:metadataDict];
}
CFRelease(source);
}
}
}];

return result;
}

+ (instancetype)metadataWithFileURL:(NSURL *)fileURL
{
if (!fileURL)
return nil;

CGImageSourceRef source = CGImageSourceCreateWithURL((__bridge CFURLRef)fileURL, NULL);
if (source == NULL)
return nil;

NSDictionary *dictionary;

NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache:@(NO)};
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(source, 0, (__bridge CFDictionaryRef)options);
if (properties) {
dictionary = (__bridge NSDictionary*)properties;
CFRelease(properties);
}

CFRelease(source);

return [self metadataWithDictionary:dictionary];
}

+ (instancetype)metadataWithImageData:(NSData *)imageData
{
if (!imageData.length)
return nil;

CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef) imageData, NULL);
if (source == NULL)
return nil;

NSDictionary *dictionary;

NSDictionary *options = @{(NSString *)kCGImageSourceShouldCache:@(NO)};
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(source, 0, (__bridge CFDictionaryRef)options);
if (properties) {
dictionary = (__bridge NSDictionary*)properties;
CFRelease(properties);
}

CFRelease(source);

return [self metadataWithDictionary:dictionary];
}

Expand All @@ -118,57 +132,65 @@ + (NSData *)dataWithImageData:(NSData *)imageData andMetadata:(SYMetadata *)meta
NSLog(@"Error: Could not create image source");
return nil;
}

CFStringRef sourceImageType = CGImageSourceGetType(source);

// create a new data object and write the new image into it
NSMutableData *data = [NSMutableData data];
CGImageDestinationRef destination = CGImageDestinationCreateWithData((__bridge CFMutableDataRef)data, sourceImageType, 1, NULL);

if (!destination) {
NSLog(@"Error: Could not create image destination");
CFRelease(source);
return nil;
}
// add the image contained in the image source to the destination, overidding the old metadata with our modified metadata

// add the image contained in the image source to the destination, overriding the old metadata with our modified metadata
CGImageDestinationAddImageFromSource(destination, source, 0, (__bridge CFDictionaryRef)metadata.generatedDictionary);
BOOL success = CGImageDestinationFinalize(destination);

if (!success)
NSLog(@"Error: Could not create data from image destination");

CFRelease(destination);
CFRelease(source);

return (success ? data : nil);
}

#pragma mark - Getting metadata

+ (NSDictionary *)dictionaryWithAssetURL:(NSURL *)assetURL
+ (NSDictionary *)dictionaryWithPHAsset:(PHAsset *)asset
{
#if !TARGET_OS_TV
__block ALAsset *assetAtUrl = nil;
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];

dispatch_semaphore_t sema = dispatch_semaphore_create(0);
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
assetAtUrl = asset;
dispatch_semaphore_signal(sema);
} failureBlock:^(NSError *error) {
dispatch_semaphore_signal(sema);
if (!asset) return nil;

__block NSDictionary *result = nil;

PHImageRequestOptions *options = [[PHImageRequestOptions alloc] init];
options.version = PHImageRequestOptionsVersionCurrent;
options.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
options.synchronous = YES;
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above: enable 'options.networkAccessAllowed = YES' to support iCloud-only assets; otherwise 'imageData' may be nil and no metadata will be returned.

Suggested change
options.synchronous = YES;
options.synchronous = YES;
options.networkAccessAllowed = YES;

Copilot uses AI. Check for mistakes.

Comment on lines +169 to +173
Copy link

Copilot AI Oct 20, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This method also uses 'options.synchronous = YES', which blocks the caller. Align with the earlier method by using an asynchronous request or ensuring calls are made off the main thread.

Copilot uses AI. Check for mistakes.
[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:asset
options:options
resultHandler:^(NSData * _Nullable imageData,
NSString * _Nullable dataUTI,
CGImagePropertyOrientation orientation,
NSDictionary * _Nullable info) {
if (imageData) {
CGImageSourceRef source = CGImageSourceCreateWithData((__bridge CFDataRef)imageData, NULL);
if (source) {
NSDictionary *metadataDict = (__bridge_transfer NSDictionary *)
CGImageSourceCopyPropertiesAtIndex(source, 0, NULL);
if (metadataDict) {
result = metadataDict;
}
CFRelease(source);
}
}
}];
dispatch_semaphore_wait(sema, DISPATCH_TIME_FOREVER);

if (!assetAtUrl)
return nil;

ALAssetRepresentation *representation = [assetAtUrl defaultRepresentation];
return [representation metadata];
#else
return nil;
#endif

return result;
}

#pragma mark - Mapping
Expand Down
Loading