美文网首页
bsbdj - XMGShowPictureViewContro

bsbdj - XMGShowPictureViewContro

作者: CaesarsTesla | 来源:发表于2016-05-09 14:36 被阅读62次

.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
屏幕快照 2016-05-09 上午11.01.52.png

然而转发按钮并没有什么用
- (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

屏幕快照 2016-05-09 下午1.37.05.png

.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

相关文章

网友评论

      本文标题:bsbdj - XMGShowPictureViewContro

      本文链接:https://www.haomeiwen.com/subject/bdcerttx.html