.h文件中
#import <UIKit/UIKit.h>
@class XMGTopic;
@interface XMGShowPictureViewController : UIViewController
/** 帖子 */
@property (nonatomic, strong) XMGTopic *topic;
@end
.m
#import "XMGShowPictureViewController.h"
#import "XMGTopic.h"
#import <UIImageView+WebCache.h>
#import <SVProgressHUD.h>
#import "XMGProgressView.h"
@interface XMGShowPictureViewController ()
@property (weak, nonatomic) IBOutlet XMGProgressView *progressView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) UIImageView *imageView;
@end
@implementation XMGShowPictureViewController
然而转发按钮并没有什么用
- (void)viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
// 添加图片
UIImageView *imageView = [[UIImageView alloc] init];
imageView.userInteractionEnabled = YES;
[imageView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)]];
[self.scrollView addSubview:imageView];
self.imageView = imageView;
// 图片尺寸
CGFloat pictureW = XMGScreenW;
CGFloat pictureH = pictureW * self.topic.height / self.topic.width;
if (pictureH > XMGScreenH) { // 图片显示高度超过一个屏幕, 需要滚动查看
imageView.frame = CGRectMake(0, 0, pictureW, pictureH);
self.scrollView.contentSize = CGSizeMake(0, pictureH);
} else {
imageView.size = CGSizeMake(pictureW, pictureH);
imageView.centerY = XMGScreenH * 0.5;
}
// 马上显示当前图片的下载进度
[self.progressView setProgress:self.topic.pictureProgress animated:YES];
// 下载图片
[imageView sd_setImageWithURL:[NSURL URLWithString:self.topic.large_image] placeholderImage:nil options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
[self.progressView setProgress:1.0 * receivedSize / expectedSize animated:NO];
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
self.progressView.hidden = YES;
}];
}
- (IBAction)back
- (IBAction)back
{
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)save {
if (self.imageView.image == nil) {
[SVProgressHUD showErrorWithStatus:@"图片并没下载完毕!"];
return;
}
// 将图片写入相册
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
image:didFinishSavingWithError:contextInfo: 将图片写入相册
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存失败!"];
} else {
[SVProgressHUD showSuccessWithStatus:@"保存成功!"];
}
}
@end
XMGProgressView
.h
#import "DALabeledCircularProgressView.h"
@interface XMGProgressView : DALabeledCircularProgressView
@end
.m
#import "XMGProgressView.h"
@implementation XMGProgressView
- (void)awakeFromNib
{
self.roundedCorners = 2;
self.progressLabel.textColor = [UIColor whiteColor];
}
- (void)setProgress:(CGFloat)progress animated:(BOOL)animated
{
[super setProgress:progress animated:animated];
NSString *text = [NSString stringWithFormat:@"%.0f%%", progress * 100];
self.progressLabel.text = [text stringByReplacingOccurrencesOfString:@"-" withString:@""];
}
@end