編輯:關於Android編程
/**
* 背景圖片
*/
@property (weak, nonatomic) IBOutlet UIImageView *pictureBackImageView;
/**
* 點擊查看大圖
*/
@property (weak, nonatomic) IBOutlet UIButton *seeBigPictureImageView;
/**
* 動圖
*/
@property (weak, nonatomic) IBOutlet UIImageView *gifImageView;
/**
* 導入模型
*/
@property (nonatomic, strong) XFJItem *topicesPicture;
#pragma mark - 模型屬性的set方法
- (void)setTopicesPicture:(XFJItem *)topicesPicture
{
_topicesPicture = topicesPicture;
[self.pictureBackImageView XFJ_originalImageURL:topicesPicture.image1 thumbnailImageURL:topicesPicture.image0 completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//判斷如果圖片下載失敗就返回
if (image == nil) return ;
//如果不是長圖就返回
if (!topicesPicture.isBigPicture) return;
//長圖來到下面
CGFloat pictureW = topicesPicture.middleFrame.size.width;
CGFloat pictureH = topicesPicture.middleFrame.size.height;
//開啟上下文
UIGraphicsBeginImageContext(CGSizeMake(pictureW, pictureH));
/*
上下文的寬度 上下文的高度
------------ = ---------------
服務器返回圖片的寬度 服務器返回圖片的高度
*/
//繪圖
[image drawInRect:CGRectMake(0, 0, pictureW, pictureW * topicesPicture.height / topicesPicture.width)];
//獲得圖片
self.pictureBackImageView.image = UIGraphicsGetImageFromCurrentImageContext();
//圖形上下文
UIGraphicsEndImageContext();
}];
//如果是短圖就隱藏點擊查看大圖按鈕
self.seeBigPictureImageView.hidden = !topicesPicture.isBigPicture;
//如果不是動圖就隱藏動圖圖標
self.gifImageView.hidden = !topicesPicture.is_gif;
}
- (IBAction)seeBigPictureClick
{
[self seeBigPicture];
}
#pragma mark - 實現輕按事件
- (void)seeBigPicture
{
//創建控制器
XFJSeeBigPicture *seePicture = [[XFJSeeBigPicture alloc] init];
//如果不寫這一句的話,會出現NaN的情況
seePicture.topicesItem = self.topicesPicture;
//model出該控制器
[self.window.rootViewController presentViewController:seePicture animated:YES completion:nil];
}
#pragma mark - 加載xib的時候回來到這個方法
- (void)awakeFromNib
{
//允許圖片接收事件
self.pictureBackImageView.userInteractionEnabled = YES;
//創建請按手勢
UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(seeBigPicture)];
//給圖片添加手勢
[self.pictureBackImageView addGestureRecognizer:gest];
}
/**
* 保存按鈕
*/
@property (weak, nonatomic) IBOutlet UIButton *saveButton;
@property (nonatomic, weak) UIImageView *imageView;
- (IBAction)backClick
{
[self dismissViewControllerAnimated:YES completion:nil];
}
#pragma mark - 添加scrollerView
- (void)setUpContentView
{
//創建UIScrollView
UIScrollView *scrollView = [[UIScrollView alloc] init];
//設置尺寸
scrollView.frame = CGRectMake(0, 0, XFJ_screenW, XFJ_screenH);
//添加手勢
[scrollView addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backClick)]];
//設置顏色
scrollView.backgroundColor = [UIColor blackColor];
//添加插入到view中
[self.view insertSubview:scrollView atIndex:0];
//調用方法
[self setUpcontentImageView:scrollView];
}
#pragma mark - 在contentView中添加UIImageView
- (void)setUpcontentImageView:(UIScrollView *)scrollView
{
UIImageView *imageView = [[UIImageView alloc] init];
//添加圖片
[imageView sd_setImageWithURL:[NSURL URLWithString:self.topicesItem.image1] placeholderImage:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//如果下載的圖片為空
if (image == nil) return ;
self.saveButton.enabled = YES;
}];
//設置尺寸
imageView.XFJ_X = 0;
imageView.XFJ_Width = scrollView.XFJ_Width;
[scrollView addSubview:imageView];
//賦值
self.imageView = imageView;
//圖片的高度
imageView.XFJ_Height = imageView.XFJ_Width * self.topicesItem.height / self.topicesItem.width;
//判斷
if (imageView.XFJ_Height >= XFJ_screenH) {
imageView.XFJ_Y = 0;
scrollView.contentSize = CGSizeMake(0, imageView.XFJ_Height);
}else{
imageView.XFJ_centerY = scrollView.XFJ_Height * 0.5;
}
CGFloat maxScale = self.topicesItem.width / self.imageView.XFJ_Width;
//判斷
if (maxScale >= 1.0) {
scrollView.maximumZoomScale = maxScale;
scrollView.delegate = self;
}
}
#pragma mark - 代理方法
//return a view that will be scaled. if delegate returns nil, nothing happens
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView
{
return self.imageView;
}
- (IBAction)savePicture
{
UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image: didFinishSavingWithError: contextInfo:), nil);
}
#pragma mark - 是否保存成功
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
[SVProgressHUD showErrorWithStatus:@"保存失敗"];
}else{
[SVProgressHUD showSuccessWithStatus:@"保存成功"];
}
}
// - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
//通過貼模型中定義的屬性帖子的類型
switch (self.topices.type) {
case XFJTopicTypeWord://段子
//將聲音;圖片;視頻圖片都隱藏
self.topicesPicture.hidden = YES;
self.topicesVideo.hidden = YES;
self.topicesVoice.hidden = YES;
break;
case XFJTopicTypePicture://圖片
//圖片出現;聲音;視頻隱藏
self.topicesVideo.hidden = YES;
self.topicesVoice.hidden = YES;
self.topicesPicture.topicesPicture = topices;
self.topicesPicture.hidden = NO;
break;
case XFJTopicTypeVideo://視頻
//視頻出現;聲音和圖片隱藏
self.topicesPicture.hidden = YES;
self.topicesVoice.hidden = YES;
self.topicesVideo.topicesVideo = topices;
self.topicesVideo.hidden = NO;
break;
case XFJTopicTypeVoice://聲音
//聲音出現;圖片和視頻隱藏
self.topicesPicture.hidden =YES;
self.topicesVoice.topicesVoice = topices;
self.topicesVoice.hidden = NO;
self.topicesVideo.hidden = YES;
break;
default:
break;
}
#pragma mark - 圖片xib懶加載
- (XFJTopicesPictureView *)topicesPicture
{
if (_topicesPicture == nil) {
//創建xib
XFJTopicesPictureView *picture = [XFJTopicesPictureView XFJ_middleXib];
//添加到tableView中的contentView中
[self.contentView addSubview:picture];
//賦值
_topicesPicture = picture;
}
return _topicesPicture;
}
#pragma mark - 實現快速創建xib的方法
+ (instancetype)XFJ_middleXib
{
return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].firstObject;
}
/*
if (緩存中有原圖) {
self.imageView.image = 原圖;
} else {
if (Wifi環境) {
下載顯示原圖
} else if (手機自帶網絡) {
if (用戶設置3G\4G環境下仍然下載原圖) {
下載顯示原圖
} else {
下載顯示小圖
}
} else {
if (緩存中有小圖) {
self.imageView.image = 小圖;
} else {
self.imageView.image = 占位圖片;
}
}
}*/
/**
* 設置下載的圖片類型(原圖或縮略圖)
*
* @param originalImageURL 原圖的URL
* @param thumbnailImageURL 縮略圖的URL
* @param placeholderImage 占位視圖
* @param completedBlock 設置完圖片回調
*/
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage completed:(SDWebImageCompletionBlock)completedBlock;
/**
* 設置下載的圖片類型(原圖或縮略圖)
*
* @param originalImageURL 原圖的URL
* @param thumbnailImageURL 縮略圖的URL
*/
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL;
/**
* 設置下載的圖片類型(原圖或縮略圖)
*
* @param originalImageURL 原圖的URL
* @param thumbnailImageURL 縮略圖的URL
* @param placeholderImage 占位視圖
*/
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage;
/**
* 設置下載的圖片類型(原圖或縮略圖)
*
* @param originalImageURL 原圖的URL
* @param thumbnailImageURL 縮略圖的URL
* @param completedBlock 設置完圖片回調
*/
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL completed:(SDWebImageCompletionBlock)completedBlock;
#pragma mark - 不需要傳占位視圖和完成需要做的事
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL
{
[self XFJ_originalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:nil completed:nil];
}
#pragma mark - 需要傳入占位視圖
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage
{
[self XFJ_originalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:placeholderImage completed:nil];
}
#pragma mark - 需要設置完圖片回調
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL completed:(SDWebImageCompletionBlock)completedBlock
{
[self XFJ_originalImageURL:originalImageURL thumbnailImageURL:thumbnailImageURL placeholderImage:nil completed:completedBlock];
}
#pragma mark - 需要設置占位視圖和完成回調
- (void)XFJ_originalImageURL:(NSString *)originalImageURL thumbnailImageURL:(NSString *)thumbnailImageURL placeholderImage:(UIImage *)placeholderImage completed:(SDWebImageCompletionBlock)completedBlock
{
//先去緩存池中或者沙盒中尋找原圖
UIImage *originalImageView = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:originalImageURL];
//判斷如果有原圖的話
if (originalImageView) {
//設置圖片(有原圖)
[self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImage completed:completedBlock];
}else{
// 內存或者沙盒中沒有原圖-->下載
AFNetworkReachabilityManager *manager = [AFNetworkReachabilityManager sharedManager];
//判斷網絡情況
if (manager.isReachableViaWiFi) {//如果是wifi
[self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImage completed:completedBlock];
}else if(manager.isReachableViaWWAN){//如果是3G或4G的話就看用戶選擇
//從沙盒中讀取用戶對是否下載原圖的配置
BOOL alwaysDownloadOriginalImage = [[NSUserDefaults standardUserDefaults] boolForKey:@"alwaysDownloadOriginalImage"];
//如果是下載原圖就直接下載
if (alwaysDownloadOriginalImage) {
[self sd_setImageWithURL:[NSURL URLWithString:originalImageURL] placeholderImage:placeholderImage completed:completedBlock];
}else{
//否則就下載小圖
[self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImage completed:completedBlock];
}
}else{
//沒有網絡,讀取緩存\沙盒中小圖
UIImage *thumbnailImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:thumbnailImageURL];
//如果有緩存\沙盒中有小圖
if (thumbnailImage) {
[self sd_setImageWithURL:[NSURL URLWithString:thumbnailImageURL] placeholderImage:placeholderImage completed:completedBlock];
}else{
[self sd_setImageWithURL:nil placeholderImage:placeholderImage completed:completedBlock];
}
}
}
}
前言:生活的艱難,更會激發對夢想的渴望,但艱難的生活卻往往會成為夢想的絆腳石上篇給大家簡單講了Webview中Native代碼與JS相互調用的方法,這篇我們再講講有關各種
前言 安卓2D繪圖,都是主要涉及到2個類:Paint,Canvas. 一,Paint Paint畫筆,用於指定圖形或者文字的顏色,大小等。 常用方法: setAntiAl
1.引子在了解OkHttp的復用連接池之前,我們首先要了解幾個概念。TCP三次握手通常我們進行HTTP連接網絡的時候我們會進行TCP的三次握手,然後傳輸數據,然後再釋放連
學習從模仿開始一個星期完成的音樂播放器基本功能,具有下一首,上一首,暫停和隨機、順序和單曲等播放,以及保存上一次播放的狀態,缺少了歌詞顯示功能。使用了andbase框架的