Skip to content

Commit 4eaa17d

Browse files
author
Jasper
committed
优化代码
1 parent 442c9fb commit 4eaa17d

27 files changed

+1819
-71
lines changed

CYUtilProject/.DS_Store

0 Bytes
Binary file not shown.

CYUtilProject/CYCache/CYWebImageCache.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,13 @@ typedef void (^CYWebImageCacheProgress)(CGFloat finishedPercent);
3838
completion:(CYWebImageCacheCompletion)completion
3939
persistence:(BOOL)persistence;
4040

41+
- (void)imageWithURL:(NSURL *)url
42+
roundCorner:(CGFloat)cornerRadius
43+
imageSize:(CGSize)imageSize
44+
progress:(CYWebImageCacheProgress)progress
45+
completion:(CYWebImageCacheCompletion)completion
46+
persistence:(BOOL)persistence;
47+
4148
#pragma mark - shared instance
4249
+ (instancetype)defaultCache;
4350

CYUtilProject/CYCache/CYWebImageCache.m

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,109 @@ - (void)imageWithURL:(NSURL *)url
104104
}];
105105
}
106106

107+
- (void)imageWithURL:(NSURL *)url
108+
roundCorner:(CGFloat)cornerRadius
109+
imageSize:(CGSize)imageSize
110+
progress:(CYWebImageCacheProgress)progress
111+
completion:(CYWebImageCacheCompletion)completion
112+
persistence:(BOOL)persistence {
113+
114+
static NSString *const roundCornerLabelPrefix = @"CYRoundCornerImage";
115+
NSString *roundCornerLabel = [roundCornerLabelPrefix stringByAppendingFormat:@"-%f-%f-%f", imageSize.width, imageSize.height, cornerRadius];
116+
117+
// 是否有切好的图片缓存
118+
NSString *roundImageUrl = [url.absoluteString stringByAppendingFormat:@"@@%@", roundCornerLabel];
119+
UIImage *cachedRoundImage = [_persistenceCache imageForKey:roundImageUrl];
120+
if (cachedRoundImage) {
121+
122+
if (completion) {
123+
124+
dispatch_async(dispatch_get_main_queue(), ^{
125+
126+
completion(cachedRoundImage, nil);
127+
});
128+
}
129+
return;
130+
}
131+
132+
// 是否有缓存原图
133+
UIImage *cachedOriginImage = [self.persistenceCache imageForKey:url.absoluteString];
134+
if (cachedOriginImage) {
135+
136+
UIImage *roundImage = [self pImageFrom:cachedOriginImage
137+
withRoundCorner:cornerRadius
138+
inSize:imageSize];
139+
[self.persistenceCache setImage:roundImage
140+
forKey:roundImageUrl
141+
toDisk:YES];
142+
143+
if (completion) {
144+
145+
dispatch_async(dispatch_get_main_queue(), ^{
146+
147+
completion(cachedRoundImage, nil);
148+
});
149+
}
150+
return;
151+
}
152+
153+
CYImageDownloadProgressBlock downloadProgress = nil;
154+
if (progress) {
155+
156+
downloadProgress = ^(int64_t bytesWritten,
157+
int64_t totalBytesWritten,
158+
int64_t totalBytesExpectedToWrite) {
159+
160+
CGFloat percent = (double)totalBytesWritten / totalBytesExpectedToWrite;
161+
if (progress) {
162+
163+
progress(percent);
164+
}
165+
};
166+
}
167+
168+
[_imageDownloader startDownloadImageWithUrl:url
169+
progress:downloadProgress
170+
completion:^(UIImage *image, NSError *error) {
171+
172+
UIImage *roundImage = nil;
173+
if (image) {
174+
175+
[self.persistenceCache setImage:image
176+
forKey:url.absoluteString
177+
toDisk:persistence];
178+
179+
roundImage = [self pImageFrom:image
180+
withRoundCorner:cornerRadius
181+
inSize:imageSize];
182+
[self.persistenceCache setImage:roundImage
183+
forKey:roundImageUrl
184+
toDisk:YES];
185+
}
186+
if (completion) {
187+
188+
completion(roundImage, error);
189+
}
190+
}];
191+
}
192+
193+
194+
195+
- (UIImage *)pImageFrom:(UIImage *)from
196+
withRoundCorner:(CGFloat)cornerRadius
197+
inSize:(CGSize)size {
198+
199+
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
200+
201+
[[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0, 0, size.width, size.height)
202+
cornerRadius:cornerRadius] addClip];
203+
204+
[from drawInRect:CGRectMake(0, 0, size.width, size.height)];
205+
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
206+
UIGraphicsEndImageContext();
207+
return newImage;
208+
}
209+
107210
#pragma mark - shared instance
108211
+ (instancetype)defaultCache {
109212

CYUtilProject/CYCache/UIImageView+CYWebImageCache.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,10 @@
2323
placeholder:(UIImage *)placeholder
2424
completion:(void (^)(UIImage *image, NSError *error))completion;
2525

26+
- (void)cy_setImageWithURL:(NSURL *)url
27+
placeholder:(UIImage *)placeholder
28+
roundCorner:(CGFloat)cornerRadius
29+
imageSize:(CGSize)imageSize
30+
completion:(void (^)(UIImage *image, NSError *error))completion;
31+
2632
@end

CYUtilProject/CYCache/UIImageView+CYWebImageCache.m

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,50 @@ - (void)cy_setImageWithURL:(NSURL *)url
7272
}
7373
}
7474

75+
- (void)cy_setImageWithURL:(NSURL *)url
76+
placeholder:(UIImage *)placeholder
77+
roundCorner:(CGFloat)cornerRadius
78+
imageSize:(CGSize)imageSize
79+
completion:(void (^)(UIImage *image, NSError *error))completion {
80+
81+
self.imageURLString = url.absoluteString;
82+
if (placeholder) {
83+
84+
self.image = placeholder;
85+
}
86+
if (url) {
87+
88+
[[CYWebImageCache defaultCache] imageWithURL:url
89+
roundCorner:cornerRadius
90+
imageSize:imageSize
91+
progress:nil
92+
completion:^(UIImage *image, NSError *error) {
93+
94+
if (image
95+
&& [self.imageURLString isEqualToString:url.absoluteString]) {
96+
97+
self.image = image;
98+
}
99+
if (completion) {
100+
101+
completion(image, error);
102+
}
103+
}
104+
persistence:YES];
105+
// [[CYWebImageCache defaultCache] imageWithURL:url
106+
// completion:^(UIImage *image, NSError *error) {
107+
//
108+
// if (image
109+
// && [self.imageURLString isEqualToString:url.absoluteString]) {
110+
//
111+
// self.image = image;
112+
// }
113+
// if (completion) {
114+
//
115+
// completion(image, error);
116+
// }
117+
// }];
118+
}
119+
}
120+
75121
@end

CYUtilProject/CYChat/CYChatView/CYChatView.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ - (void)chatInputChanged:(CYChatInputContentInputType)inputType {
372372

373373
[self animatedRefreshConstraints];
374374

375-
[_tableView scrollToBottomAnimated:YES];
375+
[self.tableView cy_scrollToBottomAnimated:YES];
376376
}
377377

378378
- (void)refreshEmotionInputLayout:(CYChatInputContentInputType)currentInputType {
@@ -461,7 +461,7 @@ - (void)refreshMoreInputLayout:(CYChatInputContentInputType)currentInputType {
461461
#pragma mark - NSNotification
462462
- (void)keyboardWillShow:(NSNotification *)notice {
463463

464-
if (_chatInputContentView.textView.isFirstResponder) {
464+
if (self.chatInputContentView.textView.isFirstResponder) {
465465

466466
CGFloat keyboardHeight = [[notice.userInfo objectForKey:@"UIKeyboardFrameEndUserInfoKey"] CGRectValue].size.height;
467467

@@ -483,7 +483,7 @@ - (void)keyboardWillShow:(NSNotification *)notice {
483483
[self replaceChatInputContentViewBottomConstraint:constraint];
484484
[self animatedRefreshConstraints];
485485

486-
[_tableView scrollToBottomAnimated:YES];
486+
[self.tableView cy_scrollToBottomAnimated:YES];
487487
}
488488
}
489489

@@ -526,7 +526,7 @@ - (void)keyboardWillChangeFrame:(NSNotification *)notice {
526526
constant:-keyboardHeight];
527527
[self replaceChatInputContentViewBottomConstraint:constraint];
528528

529-
[_tableView scrollToBottomAnimated:YES];
529+
[self.tableView cy_scrollToBottomAnimated:YES];
530530
}
531531
}
532532

CYUtilProject/CYFoundationUtils/NSArray+CYJson.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ + (NSArray *)cy_arrayFromJsonData:(NSData *)jsonData {
6868
// write to file as json string
6969
- (BOOL)cy_writeToFileAsJson:(NSString *)filePath automically:(BOOL)useAuxiliaryFile {
7070

71-
return [self writeToFileAsJson:filePath
72-
atomically:useAuxiliaryFile
73-
error:nil];
71+
return [self cy_writeToFileAsJson:filePath
72+
atomically:useAuxiliaryFile
73+
error:nil];
7474
}
7575

7676
- (BOOL)cy_writeToFileAsJson:(NSString *)filePath atomically:(BOOL)useAuxiliaryFile error:(NSError **)error {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// CYVerticalMultiPageView.h
3+
// CYUtilProject
4+
//
5+
// Created by xn011644 on 6/8/16.
6+
// Copyright © 2016 Charry. All rights reserved.
7+
//
8+
9+
#import <UIKit/UIKit.h>
10+
11+
@interface CYVerticalMultiPageScrollView : UIView
12+
13+
@property (nonatomic, strong, readonly) NSArray *contentViews;
14+
15+
@property (nonatomic, assign, readonly) NSUInteger currentIndex;
16+
17+
- (instancetype)initWithFrame:(CGRect)frame
18+
contentViews:(NSArray *)contentViews;
19+
20+
// add content view,
21+
// you can add content view at any time you want,
22+
// even after the the view has showed
23+
- (void)addContentView:(UIView *)contentView;
24+
25+
// show view
26+
- (void)showPreviousViewAnimated:(BOOL)animated;
27+
- (void)showNextViewAnimated:(BOOL)animated;
28+
- (void)showViewAtIndex:(NSUInteger)index
29+
animated:(BOOL)animated;
30+
31+
@end

0 commit comments

Comments
 (0)