diff --git a/MessageDisplayKit/Classes/Common/XHAudioPlayerHelper.m b/MessageDisplayKit/Classes/Common/XHAudioPlayerHelper.m index 27f6873..caee081 100755 --- a/MessageDisplayKit/Classes/Common/XHAudioPlayerHelper.m +++ b/MessageDisplayKit/Classes/Common/XHAudioPlayerHelper.m @@ -37,6 +37,7 @@ - (void)stopAudio { if (_player && _player.isPlaying) { [_player stop]; } + [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; [[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; if ([self.delegate respondsToSelector:@selector(didAudioPlayerStopPlay:)]) { [self.delegate didAudioPlayerStopPlay:_player]; @@ -180,11 +181,13 @@ - (void)sensorStateChange:(NSNotificationCenter *)notification { //黑屏 DLog(@"Device is close to user"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayAndRecord error:nil]; + [[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; } else { //没黑屏幕 DLog(@"Device is not close to user"); [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error:nil]; + [[AVAudioSession sharedInstance] setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; if (!_player || !_player.isPlaying) { //没有播放了,也没有在黑屏状态下,就可以把距离传感器关了 [[UIDevice currentDevice] setProximityMonitoringEnabled:NO]; diff --git a/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m b/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m index 6612e87..124491f 100644 --- a/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m +++ b/MessageDisplayKit/Classes/Common/XHVoiceRecordHelper.m @@ -90,6 +90,7 @@ - (void)cancelRecording { - (void)stopRecord { [self cancelRecording]; + [[AVAudioSession sharedInstance] setActive:NO withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:nil]; [self resetTimer]; } @@ -107,7 +108,7 @@ - (void)prepareRecordingWithPath:(NSString *)path prepareRecorderCompletion:(XHP } error = nil; - [audioSession setActive:YES error:&error]; + [audioSession setActive:YES withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error]; if(error) { DLog(@"audioSession: %@ %ld %@", [error domain], (long)[error code], [[error userInfo] description]); return; diff --git a/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.h b/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.h index a6ce3ad..d4dff87 100644 --- a/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.h +++ b/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.h @@ -10,7 +10,7 @@ #import "XHMessageBubbleFactory.h" #define kXHBubblePhotoMargin 8.0f // 上下左右的边距 - +#define kXHMaxBubblePhotoWidth 280.0f//最大图片宽度 @interface XHBubblePhotoImageView : UIView /** diff --git a/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.m b/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.m index 0aa1ec1..7dcb975 100644 --- a/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.m +++ b/MessageDisplayKit/Classes/Views/MessageContentViews/XHBubblePhotoImageView.m @@ -13,6 +13,7 @@ #import "XHConfigurationHelper.h" #import "XHMacro.h" +#import "XHMessageBubbleView.h" @interface XHBubblePhotoImageView () @@ -46,8 +47,10 @@ - (void)setMessagePhoto:(UIImage *)messagePhoto { - (void)configureMessagePhoto:(UIImage *)messagePhoto thumbnailUrl:(NSString *)thumbnailUrl originPhotoUrl:(NSString *)originPhotoUrl onBubbleMessageType:(XHBubbleMessageType)bubbleMessageType { self.bubbleMessageType = bubbleMessageType; - self.messagePhoto = messagePhoto; + if (messagePhoto) { + [self updateBubbleViewWithImage:messagePhoto]; + } if (thumbnailUrl) { WEAKSELF [self addSubview:self.activityIndicatorView]; @@ -68,25 +71,62 @@ - (void)configureMessagePhoto:(UIImage *)messagePhoto thumbnailUrl:(NSString *)t weakSelf.semaphore = nil; } } - // if image not nil if (image) { - // scale image - image = [image thumbnailImage:CGRectGetWidth(weakSelf.bounds) * 2 transparentBorder:0 cornerRadius:0 interpolationQuality:1.0]; - dispatch_async(dispatch_get_main_queue(), ^{ - // if image not nil - if (image) { - // show image - weakSelf.messagePhoto = image; - [weakSelf.activityIndicatorView stopAnimating]; - } - }); + [weakSelf updateBubbleViewWithImage:image]; } + } }]; } } +/** + * 根据图片更新BubbleView(主要设置图片大小尺寸) + */ +- (void)updateBubbleViewWithImage:(UIImage *)image { + + // scale image + CGSize size = [self needSizeForImage:image]; + image = [image resizedImage:size interpolationQuality:1.0]; + //图片太宽进行剪切,以免图片变形(截取中间的进行显示) + if (size.width > kXHMaxBubblePhotoWidth) { + image = [image croppedImage:CGRectMake((size.width - kXHMaxBubblePhotoWidth)/2, 0, kXHMaxBubblePhotoWidth, size.height)]; + size = CGSizeMake(kXHMaxBubblePhotoWidth, size.height); + } + + WEAKSELF + dispatch_async(dispatch_get_main_queue(), ^{ + // if image not nil + if (image) { + UIView *view = weakSelf.superview; + if ([view isKindOfClass:[XHMessageBubbleView class]]) { + XHMessageBubbleView * bubble = (XHMessageBubbleView *)view; + + bubble.sizeForBubblePhotoImageView = size; + [bubble layoutSubviews]; + } + // show image + weakSelf.messagePhoto = image; + [weakSelf.activityIndicatorView stopAnimating]; + } + }); +} + +/** + * 计算图片需要显示的大小,高度140计 + */ +- (CGSize)needSizeForImage:(UIImage *) photo { + // 这里需要缩放后的size + CGSize photoSize ; + + CGFloat photoWidth = photo.size.width; + CGFloat photoHeight = photo.size.height; + CGFloat realWidth = photoWidth * 140 / photoHeight; + photoSize = CGSizeMake(realWidth, 140); + return photoSize; +} + - (void)setFrame:(CGRect)frame { [super setFrame:frame]; if (self.semaphore) { diff --git a/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.h b/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.h index c363f1d..6f212b8 100644 --- a/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.h +++ b/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.h @@ -88,6 +88,11 @@ */ @property (nonatomic, strong) UIFont *font UI_APPEARANCE_SELECTOR; +/** + * 图片实际显示尺寸 + */ +@property (nonatomic,assign) CGSize sizeForBubblePhotoImageView; + /** * 初始化消息内容显示控件的方法 * diff --git a/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.m b/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.m index d455fab..acbcbdd 100644 --- a/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.m +++ b/MessageDisplayKit/Classes/Views/MessageContentViews/XHMessageBubbleView.m @@ -511,6 +511,10 @@ - (void)layoutSubviews { case XHBubbleMessageMediaTypeVideo: case XHBubbleMessageMediaTypeLocalPosition: { CGSize needPhotoSize = [XHMessageBubbleView neededSizeForPhoto:self.message.photo]; + if (!CGSizeEqualToSize(_sizeForBubblePhotoImageView,CGSizeZero)) { + needPhotoSize = _sizeForBubblePhotoImageView; + } + CGFloat paddingX = 0.0f; if (self.message.bubbleMessageType == XHBubbleMessageTypeSending) { paddingX = CGRectGetWidth(self.bounds) - needPhotoSize.width;