Skip to content

Commit e9ddf3a

Browse files
committed
optimize get image from PHAssert & fix read PHAssert data exception
1 parent 345defa commit e9ddf3a

File tree

5 files changed

+56
-27
lines changed

5 files changed

+56
-27
lines changed

QiniuDemo/Podfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
source 'https://github.com/CocoaPods/Specs.git'
22

33
target "QiniuDemo" do
4-
platform :ios, "7.0"
4+
platform :ios, "10.0"
55

66
# pod 'Qiniu', '~> 8.1.1'
77
pod 'Qiniu',:path => '../'

QiniuDemo/QiniuDemo.xcodeproj/project.pbxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@
553553
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
554554
PRODUCT_BUNDLE_IDENTIFIER = com.qiniu.QiniuDemo;
555555
PRODUCT_NAME = "$(TARGET_NAME)";
556+
STRIP_SWIFT_SYMBOLS = YES;
556557
SUPPORTS_MACCATALYST = YES;
557558
};
558559
name = Debug;
@@ -569,6 +570,7 @@
569570
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
570571
PRODUCT_BUNDLE_IDENTIFIER = com.qiniu.QiniuDemo;
571572
PRODUCT_NAME = "$(TARGET_NAME)";
573+
STRIP_SWIFT_SYMBOLS = YES;
572574
SUPPORTS_MACCATALYST = YES;
573575
};
574576
name = Release;

QiniuDemo/QiniuDemo/ViewController.m

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -124,27 +124,27 @@ - (void)uploadImageToQNFilePath:(NSString *)filePath {
124124
// }
125125
// option:uploadOption];
126126

127-
long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];
128-
NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
129-
[upManager putInputStream:stream sourceId:filePath.lastPathComponent size:fileSize fileName:filePath.lastPathComponent key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
130-
NSLog(@"info ===== %@", info);
131-
NSLog(@"resp ===== %@", resp);
132-
133-
[weakSelf changeUploadState:UploadStatePrepare];
134-
[weakSelf alertMessage:info.message];
135-
} option:uploadOption];
136-
137-
// NSURL *url = [NSURL fileURLWithPath:filePath];
138-
// PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
139-
// PHAsset *asset = [self getPHAssert];
140-
// [upManager putPHAsset:asset key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
127+
// long long fileSize = [[[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil] fileSize];
128+
// NSInputStream *stream = [NSInputStream inputStreamWithFileAtPath:filePath];
129+
// [upManager putInputStream:stream sourceId:filePath.lastPathComponent size:fileSize fileName:filePath.lastPathComponent key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
141130
// NSLog(@"info ===== %@", info);
142131
// NSLog(@"resp ===== %@", resp);
143132
//
144133
// [weakSelf changeUploadState:UploadStatePrepare];
145134
// [weakSelf alertMessage:info.message];
146-
// }
147-
// option:uploadOption];
135+
// } option:uploadOption];
136+
137+
NSURL *url = [NSURL fileURLWithPath:filePath];
138+
PHFetchResult *fetchResult = [PHAsset fetchAssetsWithALAssetURLs:@[url] options:nil];
139+
PHAsset *asset = [self getPHAssert];
140+
[upManager putPHAsset:asset key:key token:self.token complete:^(QNResponseInfo *info, NSString *key, NSDictionary *resp) {
141+
NSLog(@"info ===== %@", info);
142+
NSLog(@"resp ===== %@", resp);
143+
144+
[weakSelf changeUploadState:UploadStatePrepare];
145+
[weakSelf alertMessage:info.message];
146+
}
147+
option:uploadOption];
148148
}
149149

150150
- (PHAsset *)getPHAssert {

QiniuSDK/Utils/QNPHAssetFile.m

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,14 @@ - (NSData *)read:(long)offset
8989
NSData *data = nil;
9090
@try {
9191
[_lock lock];
92-
if (_assetData != nil) {
93-
data = [_assetData subdataWithRange:NSMakeRange(offset, (unsigned int)size)];
94-
} else {
92+
if (_assetData != nil && offset < _assetData.length) {
93+
NSInteger realSize = MIN(size, _assetData.length - offset);
94+
data = [_assetData subdataWithRange:NSMakeRange(offset, realSize)];
95+
} else if (_file != nil && offset < _fileSize) {
9596
[_file seekToFileOffset:offset];
9697
data = [_file readDataOfLength:size];
98+
} else {
99+
data = [NSData data];
97100
}
98101
} @catch (NSException *exception) {
99102
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
@@ -157,11 +160,30 @@ - (void)getImageInfo {
157160
options.networkAccessAllowed = NO;
158161
options.synchronous = YES;
159162

160-
[[PHImageManager defaultManager] requestImageDataForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
161-
self.assetData = imageData;
162-
self.fileSize = imageData.length;
163-
self.hasRealFilePath = NO;
164-
}];
163+
#if TARGET_OS_MACCATALYST
164+
if (@available(macOS 10.15, *)) {
165+
[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, CGImagePropertyOrientation orientation, NSDictionary *info) {
166+
self.assetData = imageData;
167+
self.fileSize = imageData.length;
168+
self.hasRealFilePath = NO;
169+
}];
170+
}
171+
#else
172+
if (@available(iOS 13, *)) {
173+
[[PHImageManager defaultManager] requestImageDataAndOrientationForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, CGImagePropertyOrientation orientation, NSDictionary *info) {
174+
self.assetData = imageData;
175+
self.fileSize = imageData.length;
176+
self.hasRealFilePath = NO;
177+
}];
178+
} else {
179+
[[PHImageManager defaultManager] requestImageDataForAsset:self.phAsset options:options resultHandler:^(NSData *imageData, NSString *dataUTI, UIImageOrientation orientation, NSDictionary *info) {
180+
self.assetData = imageData;
181+
self.fileSize = imageData.length;
182+
self.hasRealFilePath = NO;
183+
}];
184+
}
185+
#endif
186+
165187
}
166188

167189
- (void)getVideoInfo {

QiniuSDK/Utils/QNPHAssetResource.m

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,16 @@ - (NSData *)read:(long)offset
6262
NSData *data = nil;
6363
@try {
6464
[_lock lock];
65-
NSRange subRange = NSMakeRange(offset, size);
6665
if (!self.assetData) {
6766
self.assetData = [self fetchDataFromAsset:self.phAssetResource error:error];
6867
}
69-
data = [self.assetData subdataWithRange:subRange];
68+
69+
if (_assetData != nil && offset < _assetData.length) {
70+
NSInteger realSize = MIN(size, _assetData.length - offset);
71+
data = [_assetData subdataWithRange:NSMakeRange(offset, realSize)];
72+
} else {
73+
data = [NSData data];
74+
}
7075
} @catch (NSException *exception) {
7176
*error = [NSError errorWithDomain:NSCocoaErrorDomain code:kQNFileError userInfo:@{NSLocalizedDescriptionKey : exception.reason}];
7277
NSLog(@"read file failed reason: %@ \n%@", exception.reason, exception.callStackSymbols);

0 commit comments

Comments
 (0)