Just My Life & My Work

上週有嘗試過PHAssetmodificationDate和MD5的Image Hash,來判斷兩影像是否一樣,不過這兩個值不太可靠,因為會有其他因素讓兩個值改變⋯⋯

於是乎,就必須找其他方法替代!這次找到檔案大小,透過PHAssetResourcefileSize,是可以當作影像是否有被修改的指標。為什麼呢?因為影像一旦被處理過,就會再次存檔,此時檔案大小會不一樣~儘管此數值可能會一模一樣,不過機率實在太低,99.999%不會同數值。😎

透過PHAsset來取得PHAssetResource:

PHAsset *phAsset = (PHAsset *)self.libraryAsset;
NSArray *resourceArr = [PHAssetResource assetResourcesForAsset:phAsset];

.

如果是有被內建相簿App修改過的影像,則會取得3個項目:

<__NSArrayM 0x2827c8b10>(
<PHAssetResource: 0x2819ce520> {
    type: adjustment
    uti: com.apple.property-list
    filename: Adjustments.plist
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/PhotoData/Mutations/DCIM/110APPLE/IMG_0386/Adjustments/Adjustments.plist
    width: 0
    height: 0
    fileSize: 3763
    analysisType: unavailable
    cplResourceType: Unknown
    isCurrent: NO
},
<PHAssetResource: 0x2819cde60> {
    type: photo
    uti: public.jpeg
    filename: IMG_0386.JPG
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/DCIM/110APPLE/IMG_0386.JPG
    width: 2316
    height: 3088
    fileSize: 1542471
    analysisType: never-download
    cplResourceType: Original
    isCurrent: YES
},
<PHAssetResource: 0x2819cc2d0> {
    type: photo_full
    uti: public.jpeg
    filename: FullSizeRender.jpg
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/PhotoData/Mutations/DCIM/110APPLE/IMG_0386/Adjustments/FullSizeRender.jpg
    width: 1943
    height: 2339
    fileSize: 1590258
    analysisType: unavailable
    cplResourceType: JPEGFull
    isCurrent: NO
}
)

.

若再次修改此影像,依然會取得3個項目:

<__NSArrayM 0x28242bba0>(
<PHAssetResource: 0x2819f6910> {
    type: adjustment
    uti: com.apple.property-list
    filename: Adjustments.plist
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/PhotoData/Mutations/DCIM/110APPLE/IMG_0386/Adjustments/Adjustments.plist
    width: 0
    height: 0
    fileSize: 3817
    analysisType: unavailable
    cplResourceType: Unknown
    isCurrent: NO
},
<PHAssetResource: 0x2819f7de0> {
    type: photo
    uti: public.jpeg
    filename: IMG_0386.JPG
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/DCIM/110APPLE/IMG_0386.JPG
    width: 2316
    height: 3088
    fileSize: 1542471
    analysisType: never-download
    cplResourceType: Original
    isCurrent: YES
},
<PHAssetResource: 0x2819f4000> {
    type: photo_full
    uti: public.jpeg
    filename: FullSizeRender.jpg
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/PhotoData/Mutations/DCIM/110APPLE/IMG_0386/Adjustments/FullSizeRender.jpg
    width: 1943
    height: 2339
    fileSize: 1372650
    analysisType: unavailable
    cplResourceType: JPEGFull
    isCurrent: NO
}
)

.

可以發現type: photo_full的項目,僅有fileSize數值不一樣,這就是我們可以拿來判斷影像是否被修改的指標!

若影像沒有被修改,或是修改後回復,則僅會取得1個項目:

<PHAssetResource: 0x2819f7de0> {
    type: photo
    uti: public.jpeg
    filename: IMG_0386.JPG
    asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001
    locallyAvailable: YES
    fileURL: file:///var/mobile/Media/DCIM/110APPLE/IMG_0386.JPG
    width: 2316
    height: 3088
    fileSize: 1542471
    analysisType: never-download
    cplResourceType: Original
    isCurrent: YES
},

.

原始相片就是type: photo這個項目。

特別需要注意的是「相片位址」,無論是原始影像或是被修改過的影像,相片位址一模一樣:

asset: FC68FF8A-7FD5-4D89-9025-7F942BC5D058/L0/001

PHAssetResourceType是用來判斷來源類型:

  • 原始相片:PHAssetResourceTypePhoto
  • 被修改相片:PHAssetResourceTypeFullSizePhoto
  • 修改記錄檔:PHAssetResourceTypeAdjustmentData
typedef NS_ENUM(NSInteger, PHAssetResourceType) {
    PHAssetResourceTypePhoto                             = 1,
    PHAssetResourceTypeVideo                             = 2,
    PHAssetResourceTypeAudio                             = 3,
    PHAssetResourceTypeAlternatePhoto                    = 4,
    PHAssetResourceTypeFullSizePhoto                     = 5,
    PHAssetResourceTypeFullSizeVideo                     = 6,
    PHAssetResourceTypeAdjustmentData                    = 7,
    PHAssetResourceTypeAdjustmentBasePhoto               = 8,
    PHAssetResourceTypePairedVideo API_AVAILABLE(ios(9.1)) = 9,
    PHAssetResourceTypeFullSizePairedVideo API_AVAILABLE(macos(10.15), ios(10)) = 10,
    PHAssetResourceTypeAdjustmentBasePairedVideo API_AVAILABLE(macos(10.15), ios(10)) = 11,
    PHAssetResourceTypeAdjustmentBaseVideo API_AVAILABLE(macos(10.15), ios(13)) = 12,

} API_AVAILABLE(ios(9));

.

解決事情有很多方法,既然沒有最佳解,那麼就用較佳解吧!若之後有更好的解法,再來改這一段判斷演算法囉~🤠

隨意留個言吧:)~

在下方填入你的資料或按右方圖示以社群網站登入:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Google photo

您的留言將使用 Google 帳號。 登出 /  變更 )

Twitter picture

您的留言將使用 Twitter 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

標籤雲

<span>%d</span> 位部落客按了讚: