Skip to content

Commit b69b887

Browse files
committed
发冒泡时,对 iCloud 图片的下载处理
1 parent 6f30b61 commit b69b887

File tree

8 files changed

+88
-14
lines changed

8 files changed

+88
-14
lines changed

Coding_iOS/Coding_iOS-Info.plist

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<key>CFBundlePackageType</key>
1818
<string>APPL</string>
1919
<key>CFBundleShortVersionString</key>
20-
<string>4.9.5</string>
20+
<string>4.9.8</string>
2121
<key>CFBundleSignature</key>
2222
<string>????</string>
2323
<key>CFBundleURLTypes</key>
@@ -37,7 +37,7 @@
3737
</dict>
3838
</array>
3939
<key>CFBundleVersion</key>
40-
<string>4.9.5.201712281800</string>
40+
<string>4.9.8.201801041500</string>
4141
<key>ITSAppUsesNonExemptEncryption</key>
4242
<false/>
4343
<key>LSApplicationQueriesSchemes</key>

Coding_iOS/Controllers/TweetSendViewController.m

+6
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,12 @@ - (BOOL)isEmptyTweet{
322322
}
323323

324324
- (void)sendTweet{
325+
for (TweetImage *tImg in _curTweet.tweetImages) {
326+
if (tImg.downloadState == TweetImageDownloadStateIng) {
327+
[NSObject showHudTipStr:@"iCloud 图片尚未下载完毕"];
328+
return;
329+
}
330+
}
325331
_curTweet.tweetContent = [_curTweet.tweetContent aliasedString];
326332
if (_sendNextTweet) {
327333
_sendNextTweet(_curTweet);

Coding_iOS/Models/Tweet.h

+9
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,19 @@ typedef NS_ENUM(NSInteger, TweetImageUploadState)
9595
TweetImageUploadStateFail
9696
};
9797

98+
typedef NS_ENUM(NSInteger, TweetImageDownloadState)
99+
{
100+
TweetImageDownloadStateInit = 0,
101+
TweetImageDownloadStateIng,
102+
TweetImageDownloadStateSuccess,
103+
TweetImageDownloadStateFail
104+
};
105+
98106
@interface TweetImage : NSObject
99107
@property (readwrite, nonatomic, strong) UIImage *image, *thumbnailImage;
100108
@property (strong, nonatomic) NSString *assetLocalIdentifier;
101109
@property (assign, nonatomic) TweetImageUploadState uploadState;
110+
@property (assign, nonatomic) TweetImageDownloadState downloadState;
102111
@property (readwrite, nonatomic, strong) NSString *imageStr;
103112
+ (instancetype)tweetImageWithAssetLocalIdentifier:(NSString *)localIdentifier;
104113
+ (instancetype)tweetImageWithAssetLocalIdentifier:(NSString *)localIdentifier andImage:(UIImage *)image;

Coding_iOS/Models/Tweet.m

+23-12
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ - (void)saveSendData{
196196
NSMutableDictionary *tweetImagesDict = [NSMutableDictionary new];
197197
for (int i = 0; i < [self.tweetImages count]; i++) {
198198
TweetImage *tImg = [self.tweetImages objectAtIndex:i];
199-
if (tImg.image) {
200-
NSString *imgNameStr = [NSString stringWithFormat:@"%@_%d.jpg", dataPath, i];
201-
if (tImg.assetLocalIdentifier) {
202-
[tweetImagesDict setObject:tImg.assetLocalIdentifier forKey:imgNameStr];
203-
}
199+
NSString *imgNameStr = [NSString stringWithFormat:@"%@_%d.jpg", dataPath, i];
200+
if (tImg.assetLocalIdentifier) {
201+
[tweetImagesDict setObject:tImg.assetLocalIdentifier forKey:imgNameStr];
202+
}
203+
if (tImg.downloadState == TweetImageDownloadStateSuccess) {
204204
[NSObject saveImage:tImg.image imageName:imgNameStr inFolder:dataPath];
205205
}
206206
}
@@ -223,13 +223,15 @@ - (void)loadSendData{
223223
_tweetImages = [NSMutableArray new];
224224
_selectedAssetLocalIdentifiers = [NSMutableArray new];
225225
[tweetImagesDict enumerateKeysAndObjectsUsingBlock:^(NSString *key, NSString *obj, BOOL *stop) {
226-
NSURL *assetURL = [NSURL URLWithString:obj];
227226
NSData *imageData = [NSObject loadImageDataWithName:key inFolder:dataPath];
227+
TweetImage *tImg;
228228
if (imageData) {
229-
TweetImage *tImg = [TweetImage tweetImageWithAssetLocalIdentifier:obj andImage:[UIImage imageWithData:imageData]];
230-
[self.tweetImages addObject:tImg];
231-
[self.selectedAssetLocalIdentifiers addObject:assetURL];
229+
tImg = [TweetImage tweetImageWithAssetLocalIdentifier:obj andImage:[UIImage imageWithData:imageData]];
230+
}else{
231+
tImg = [TweetImage tweetImageWithAssetLocalIdentifier:obj];
232232
}
233+
[self.tweetImages addObject:tImg];
234+
[self.selectedAssetLocalIdentifiers addObject:obj];
233235
}];
234236
}
235237

@@ -380,26 +382,35 @@ + (instancetype)tweetImageWithAssetLocalIdentifier:(NSString *)localIdentifier{
380382
if (!localIdentifier) {
381383
return nil;
382384
}
383-
PHAsset *asset = [PHAsset fetchAssetsWithLocalIdentifiers:@[localIdentifier] options:nil].firstObject;
385+
PHAsset *asset = [PHAsset assetWithLocalIdentifier:localIdentifier];
384386
if (!asset) {
385387
return nil;
386388
}
387389
TweetImage *tweetImg = [[TweetImage alloc] init];
388390
tweetImg.uploadState = TweetImageUploadStateInit;
389391
tweetImg.assetLocalIdentifier = localIdentifier;
390-
tweetImg.image = [PHAsset loadImageWithLocalIdentifier:localIdentifier];
391-
tweetImg.thumbnailImage = [tweetImg.image scaledToSize:CGSizeMake(kScaleFrom_iPhone5_Desgin(70), kScaleFrom_iPhone5_Desgin(70)) highQuality:YES];
392+
tweetImg.thumbnailImage = asset.loadThumbnailImage;
393+
tweetImg.downloadState = TweetImageDownloadStateIng;
394+
[asset loadImageWithProgressHandler:nil resultHandler:^(UIImage *result, NSDictionary *info) {
395+
DebugLog(@"\n----------\nresultHandler: %@", info);
396+
tweetImg.image = result;
397+
tweetImg.downloadState = result? TweetImageDownloadStateSuccess: TweetImageDownloadStateFail;
398+
}];
392399
return tweetImg;
393400
}
394401

395402
+ (instancetype)tweetImageWithAssetLocalIdentifier:(NSString *)localIdentifier andImage:(UIImage *)image{
396403
TweetImage *tweetImg = [[TweetImage alloc] init];
397404
tweetImg.uploadState = TweetImageUploadStateInit;
405+
tweetImg.downloadState = TweetImageUploadStateSuccess;
398406
tweetImg.assetLocalIdentifier = localIdentifier;
399407
tweetImg.image = image;
400408
tweetImg.thumbnailImage = [image scaledToSize:CGSizeMake(kScaleFrom_iPhone5_Desgin(70), kScaleFrom_iPhone5_Desgin(70)) highQuality:YES];
401409
return tweetImg;
402410
}
403411

412+
- (UIImage *)image{
413+
return _image ?: _thumbnailImage;
414+
}
404415

405416
@end

Coding_iOS/Util/OC_Category/PHAsset+Common.h

+2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
+ (PHAsset *)assetWithLocalIdentifier:(NSString *)localIdentifier;
1414
+ (UIImage *)loadImageWithLocalIdentifier:(NSString *)localIdentifier;
1515

16+
- (UIImage *)loadThumbnailImage;
1617
- (UIImage *)loadImage;
1718
- (NSData *)loadImageData;
1819
- (NSString *)fileName;
1920

21+
- (void)loadImageWithProgressHandler:(PHAssetImageProgressHandler)progressHandler resultHandler:(void (^)(UIImage *result, NSDictionary *info))resultHandler;
2022
@end

Coding_iOS/Util/OC_Category/PHAsset+Common.m

+29
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,29 @@ + (UIImage *)loadImageWithLocalIdentifier:(NSString *)localIdentifier{
1919
return [self assetWithLocalIdentifier:localIdentifier].loadImage;
2020
}
2121

22+
- (UIImage *)loadThumbnailImage{
23+
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
24+
imageOptions.synchronous = YES;
25+
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
26+
imageOptions.resizeMode = PHImageRequestOptionsResizeModeFast;
27+
imageOptions.networkAccessAllowed = YES;
28+
PHImageManager *imageManager = [PHImageManager defaultManager];
29+
CGFloat width = ((kScreen_Width - 15*2- 10*3)/4) * [UIScreen mainScreen].scale;
30+
CGSize targetSize =CGSizeMake(width, width);
31+
__block UIImage *assetImage;
32+
[imageManager requestImageForAsset:self targetSize:targetSize contentMode:PHImageContentModeAspectFill options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
33+
assetImage = result;
34+
}];
35+
return assetImage;
36+
}
37+
38+
2239
- (UIImage *)loadImage{
2340
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
2441
imageOptions.synchronous = YES;
2542
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
2643
imageOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
44+
imageOptions.networkAccessAllowed = YES;
2745
PHImageManager *imageManager = [PHImageManager defaultManager];
2846
__block UIImage *assetImage;
2947
[imageManager requestImageForAsset:self targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:imageOptions resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
@@ -37,6 +55,7 @@ - (NSData *)loadImageData{
3755
imageOptions.synchronous = YES;
3856
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
3957
imageOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
58+
imageOptions.networkAccessAllowed = YES;
4059
PHImageManager *imageManager = [PHImageManager defaultManager];
4160
__block NSData *assetData;
4261
[imageManager requestImageDataForAsset:self options:imageOptions resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) {
@@ -55,5 +74,15 @@ - (NSString *)fileName{
5574
return fileName;
5675
}
5776

77+
- (void)loadImageWithProgressHandler:(PHAssetImageProgressHandler)progressHandler resultHandler:(void (^)(UIImage *result, NSDictionary *info))resultHandler{
78+
PHImageRequestOptions *imageOptions = [[PHImageRequestOptions alloc] init];
79+
imageOptions.synchronous = NO;
80+
imageOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
81+
imageOptions.resizeMode = PHImageRequestOptionsResizeModeExact;
82+
imageOptions.networkAccessAllowed = YES;
83+
imageOptions.progressHandler = progressHandler;
84+
PHImageManager *imageManager = [PHImageManager defaultManager];
85+
[imageManager requestImageForAsset:self targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeDefault options:imageOptions resultHandler:resultHandler];
86+
}
5887

5988
@end

Coding_iOS/Views/CCell/TweetSendImageCCell.h

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#import "Tweets.h"
1313

1414
@interface TweetSendImageCCell : UICollectionViewCell
15+
@property (strong, nonatomic) UIActivityIndicatorView *activityIndicator;
1516
@property (strong, nonatomic) UIImageView *imgView;
1617
@property (strong, nonatomic) UIButton *deleteBtn;
1718
@property (strong, nonatomic) TweetImage *curTweetImg;

Coding_iOS/Views/CCell/TweetSendImageCCell.m

+16
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,23 @@ - (void)setCurTweetImg:(TweetImage *)curTweetImg{
5050
[_deleteBtn addTarget:self action:@selector(deleteBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
5151
[self.contentView addSubview:_deleteBtn];
5252
}
53+
if (!_activityIndicator) {
54+
_activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
55+
_activityIndicator.hidesWhenStopped = YES;
56+
[self.contentView addSubview:_activityIndicator];
57+
[_activityIndicator mas_makeConstraints:^(MASConstraintMaker *make) {
58+
make.center.equalTo(self.contentView);
59+
}];
60+
}
5361
RAC(self.imgView, image) = [RACObserve(self.curTweetImg, thumbnailImage) takeUntil:self.rac_prepareForReuseSignal];
62+
__weak typeof(self) weakSelf = self;
63+
[[RACObserve(self.curTweetImg, downloadState) takeUntil:self.rac_prepareForReuseSignal] subscribeNext:^(NSNumber *x) {
64+
if (x.integerValue == TweetImageDownloadStateIng) {
65+
[weakSelf.activityIndicator startAnimating];
66+
}else{
67+
[weakSelf.activityIndicator stopAnimating];
68+
}
69+
}];
5470
_deleteBtn.hidden = NO;
5571
}else{
5672
_imgView.image = [UIImage imageNamed:@"addPictureBgImage"];

0 commit comments

Comments
 (0)