`

自己写的一个图片展示

 
阅读更多
MediaComponentView.h

//
//  MediaComponentView.h
//  MediaComponent
//
//  Created by Jason Wang on 10/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import <UIKit/UIKit.h>


@interface MediaComponentView : UIView <UITableViewDelegate,UITableViewDataSource> {
	NSArray *images;
	
	NSUInteger index;
	
	CGRect imageViewRect;
	
	//UITableView *_tableView;
}

@property(nonatomic,readonly) NSUInteger index; 

- (id)initWithFrame:(CGRect)frame images:(NSArray *)imagesAry;
- (void)show;

@end


MediaComponentView.m

//
//  MediaComponentView.m
//  MediaComponent
//
//  Created by Jason Wang on 10/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#define MAINVIEW_PER 0.75
#define LISTVIEW_PER 0.25
#define TABLE_GAP_HEIGHT_PER 0.1

#define MAIN_GAP_WIDTH 3
#define IMAGE_GAP_WIDTH 8


#define TABLE_HEIGHT 0.8

#import "MediaComponentView.h"
#import "MediaComponentCell.h"

@interface MediaComponentView()
- (void)showMainImage:(NSInteger)_index;
- (void)showNewImage:(id)sender;
@end


@implementation MediaComponentView

@synthesize index;

- (id)initWithFrame:(CGRect)frame images:(NSArray *)imagesAry{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code here.
		images = [[NSArray alloc] initWithArray:imagesAry];
		index = 0;
    }
    return self;
}

- (void)drawRect:(CGRect)dirtyRect {
    // Drawing code here.
	
}

- (void)show {
	CGRect rect = self.frame;
	self.backgroundColor = [UIColor colorWithWhite:0.8 alpha:1.0];
	if ([images count] > 0) {
		index = 0;
		
		//Get the first image in images arry
		UIImage *image = [UIImage imageNamed:[[images objectAtIndex:index] objectForKey:@"image"]];
		NSString *comment = [[images objectAtIndex:index] objectForKey:@"comment"];
		
		UIView *mainImageView = [[UIView alloc] initWithFrame:CGRectMake(MAIN_GAP_WIDTH, MAIN_GAP_WIDTH, rect.size.width - MAIN_GAP_WIDTH * 2 , rect.size.height - MAIN_GAP_WIDTH * 2)];
		mainImageView.backgroundColor = [UIColor whiteColor];
		CGRect mainImageViewRect = mainImageView.frame;
		mainImageView.tag = 1;
		
		//init the UIImageView
		UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
		//set the tag to 1, in order to get the UIImageView later
		imageView.tag = 2;
		imageView.contentMode = UIViewContentModeScaleAspectFit; 
		imageView.frame = CGRectMake(IMAGE_GAP_WIDTH, IMAGE_GAP_WIDTH, mainImageViewRect.size.width - IMAGE_GAP_WIDTH * 2, mainImageViewRect.size.height - IMAGE_GAP_WIDTH * 2);
		imageViewRect = imageView.frame;
		
		if ([images count] > 1) {
			mainImageViewRect = CGRectMake(MAIN_GAP_WIDTH, MAIN_GAP_WIDTH, rect.size.width * MAINVIEW_PER - MAIN_GAP_WIDTH, rect.size.height - MAIN_GAP_WIDTH * 2);
			imageViewRect = CGRectMake(IMAGE_GAP_WIDTH, IMAGE_GAP_WIDTH, mainImageViewRect.size.width - IMAGE_GAP_WIDTH * 2, mainImageViewRect.size.height - IMAGE_GAP_WIDTH * 2);
			
			UIView *listView = [[UIView alloc] initWithFrame:CGRectMake(rect.size.width * MAINVIEW_PER + IMAGE_GAP_WIDTH, MAIN_GAP_WIDTH, rect.size.width * LISTVIEW_PER - IMAGE_GAP_WIDTH * 2, rect.size.height - MAIN_GAP_WIDTH * 2)];
			listView.backgroundColor = [UIColor clearColor];
			CGRect listViewRect = listView.frame;
			
			UITableView *_tableView = [[UITableView alloc] initWithFrame:CGRectMake(IMAGE_GAP_WIDTH, listViewRect.size.height * TABLE_GAP_HEIGHT_PER, listViewRect.size.width - IMAGE_GAP_WIDTH * 2, listViewRect.size.height * (1 - TABLE_GAP_HEIGHT_PER * 2)) style:UITableViewStylePlain];
			_tableView.backgroundColor = [UIColor blackColor];	
			_tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
			_tableView.dataSource = self;
			_tableView.delegate = self;
			
			[listView addSubview:_tableView];
			[_tableView release]; 
			
			[self addSubview:listView];
			[listView release];
		}
		
		if ((comment && ![comment isEqualToString:@""]) || [images count] > 1) {
			imageViewRect = CGRectMake(imageViewRect.origin.x, imageViewRect.origin.y, imageViewRect.size.width, imageViewRect.size.height * 7 / 8 - IMAGE_GAP_WIDTH);
			
			CGRect commentViewRect = CGRectMake(imageViewRect.origin.x, imageViewRect.origin.y + imageViewRect.size.height + IMAGE_GAP_WIDTH, imageViewRect.size.width, imageViewRect.size.height / 8);
			
			UITextView * textView = [[UITextView alloc] initWithFrame:commentViewRect];
			textView.textAlignment = UITextAlignmentCenter;
			textView.text = comment;
			textView.editable = NO;
			textView.backgroundColor = [UIColor clearColor];
			textView.font = [UIFont systemFontOfSize:16];
			textView.tag = 3;
			[mainImageView addSubview:textView];
			[textView release];
		}
		mainImageView.frame = mainImageViewRect;
		imageView.frame= imageViewRect;
		
		[mainImageView addSubview:imageView];
		[imageView release];
		
		[self addSubview:mainImageView];
		[mainImageView release];
	}
}

- (void)showMainImage:(NSInteger)_index {
	UIView *mainImageView = [self viewWithTag:1];
	UIImageView *imageView = (UIImageView *)[mainImageView viewWithTag:2];
	imageViewRect = imageView.frame;
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.5f];
	[UIView setAnimationDelegate:self];
	[UIView setAnimationDidStopSelector:@selector(showNewImage:)];
	imageView.frame = CGRectMake(imageViewRect.origin.x, imageViewRect.origin.y, imageViewRect.size.width, 0);
	[UIView commitAnimations];
}

- (void)showNewImage:(id)sender {
	UIView *mainImageView = [self viewWithTag:1];
	UITextView *textView = (UITextView *)[mainImageView viewWithTag:3];
	textView.text = [[images objectAtIndex:index] objectForKey:@"comment"];
	UIImageView *imageView = (UIImageView *)[mainImageView viewWithTag:2];
	NSString *imagePath = [[images objectAtIndex:index] objectForKey:@"image"];
	[imageView setImage:[UIImage imageNamed:imagePath]];
	[UIView beginAnimations:nil context:NULL];
	[UIView setAnimationDuration:0.5f];
	imageView.frame = imageViewRect;
	[UIView commitAnimations];
}

#pragma mark UITableView

- (void)tableView:(UITableView *)tableView didDeselectRowAtIndexPath:(NSIndexPath *)indexPath {
	MediaComponentCell *cell = (MediaComponentCell *)[tableView cellForRowAtIndexPath:indexPath];
	[cell deSelectedNow];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
	return self.frame.size.width * LISTVIEW_PER - IMAGE_GAP_WIDTH * 4;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
	return 1;
}

- (NSInteger)tableView:(UITableView *)table numberOfRowsInSection:(NSInteger)section{
	return [images count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
	static NSString * CellIdentifier = @"Cell";
	MediaComponentCell *cell = (MediaComponentCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) {
		cell = [[[MediaComponentCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
	}
	[cell setData:[[images objectAtIndex:indexPath.row] objectForKey:@"image"] index:indexPath.row];
	cell.selectionStyle = UITableViewCellSelectionStyleNone;
	cell.delegateView = self;
	return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
	if (index == indexPath.row) return;
	index = indexPath.row;
	MediaComponentCell *cell = (MediaComponentCell *)[tableView cellForRowAtIndexPath:indexPath];
	[cell selectedNow];
	if (index != 0) {
		MediaComponentCell *firstCell = (MediaComponentCell *)[tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
		[firstCell deSelectedNow]; 
	}
	[self showMainImage:indexPath.row];
}

- (void)dealloc {
	[images release];
	[super dealloc];
}

@end


MediaComponentCell.h

//
//  MediaComponentCell.h
//  MediaComponent
//
//  Created by Jason Wang on 10/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>


@interface MediaComponentCell : UITableViewCell {
	NSString *imageFile;
	
	NSUInteger cellAtIndex;
	
	id delegateView;
}

@property(nonatomic,assign) id delegateView;

- (void)setData:(NSString *)imagePath index:(NSUInteger)_index;

- (void)selectedNow;

- (void)deSelectedNow;

@end


MediaComponentCell.m

//
//  MediaComponentCell.m
//  MediaComponent
//
//  Created by Jason Wang on 10/7/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MediaComponentCell.h"
#import "MediaComponentView.h"

@interface MediaComponentCell()
- (UIImage *)convertImageToGrayScale:(UIImage *)image;
@end



@implementation MediaComponentCell

@synthesize delegateView;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    if ((self = [super initWithStyle:style  reuseIdentifier:reuseIdentifier])) {
        // Initialization code
		
    }
    return self;
}

- (void)layoutSubviews {
	[super layoutSubviews];
	UIImageView *iView = (UIImageView *)[self.contentView viewWithTag:11];
	
	CGRect contentRect = self.contentView.frame;
	iView.frame = CGRectMake(contentRect.origin.x + 2, contentRect.origin.y + 2, contentRect.size.width - 4, contentRect.size.height - 4);
}

- (void)setData:(NSString *)imagePath index:(NSUInteger)_index {
	UIView *contentView = self.contentView;
	cellAtIndex = _index;
	if (imageFile) {
		[imageFile release];
		imageFile = nil;
	}
	imageFile = [[NSString alloc] initWithString:imagePath];
	UIImage *mainImage = [UIImage imageNamed:imageFile];
	MediaComponentView *view = (MediaComponentView *)delegateView;
	if (view.index != cellAtIndex) {
		mainImage = [self convertImageToGrayScale:mainImage];
	}
	UIImageView *iView = (UIImageView *)[contentView viewWithTag:11];
	if (!iView) {
		iView = [[UIImageView alloc] initWithImage:mainImage];
		iView.contentMode = UIViewContentModeScaleAspectFit;
		iView.tag = 11;
		[contentView addSubview:iView];
		[iView release];
	} else {
		[iView setImage:mainImage];
	}

}

- (void)selectedNow {
	UIImage *mainImage = [UIImage imageNamed:imageFile];
	UIImageView *iView = (UIImageView *)[self.contentView viewWithTag:11];
	[iView setImage:mainImage];
}

- (void)deSelectedNow {
	UIImage *origionalImage = [UIImage imageNamed:imageFile];
	UIImage *mainImage = [self convertImageToGrayScale:origionalImage];
	UIImageView *iView = (UIImageView *)[self.contentView viewWithTag:11];
	[iView setImage:mainImage];
}

- (UIImage *)convertImageToGrayScale:(UIImage *)image {
	CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height);
	
	CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
	
	CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
	
	CGContextDrawImage(context, imageRect, [image CGImage]);
	
	CGImageRef imageRef = CGBitmapContextCreateImage(context);
	
	UIImage *newImage = [UIImage imageWithCGImage:imageRef];
	
	CGColorSpaceRelease(colorSpace);
	CGContextRelease(context);
	CFRelease(imageRef);
	
	return newImage;
}


- (void)dealloc {
	[imageFile release]; 
    [super dealloc];
}


@end
0
1
分享到:
评论

相关推荐

    jQuery插件开发 图片查看插件(自己开发写的)

    在jQuery框架下,开发一个自己的插件(图片查看),点击图片,可以弹出一个遮罩层,点击可以查看图片上下,都写到了插件里面,对初学jQuery插件开发者有很大的帮助,并且可以在这基础上扩展,是个很好的开始。

    超级漂亮 as3.0 产品展示,带源码!自己写的

    超级漂亮 as3.0 产品展示,带源码!自己写的

    highCharts 展示图片示例

    highCharts 示例 demo highCharts入门 highCharts 自己 按照API文档 写的 ...另一篇 是一个 fusioncharts 的例子 需要的朋友看看 另外 怎么用highcharts 和 fusioncharts 画 雷达图 ,仪表图 ,会的 朋友 ,赐教一下!

    基于微信小程序实现多肉植物图鉴图片展示小程序项目源码

    小程序是一个易上手的东西, 对于新手来说,多看官方文档,可以初步做出比较完整的小程序,正是因为简单上手,功能实现简单,小程序是越来越火,商业价值也越来越大。 1. 微信web开发者工具:微信小程序官网 这是个...

    基于微信小程序实现多肉植物图鉴图片展示小程序项目源码分享

    小程序是一个易上手的东西, 对于新手来说,多看官方文档,可以初步做出比较完整的小程序,正是因为简单上手,功能实现简单,小程序是越来越火,商业价值也越来越大。 1. 微信web开发者工具:微信小程序官网 这是个...

    PhotoFun-图趣超轻图片网站系统 图片网站源码 自适应图片网站源码

    PhotoFun-图趣是一款由淘码岛出品的自适应多语言图片网站系统。PhotoFun-图趣最大的特点就是轻量级,整个...如果您喜欢PhotoFun,请将它介绍给自己的朋友,或者帮助他人安装一个PhotoFun,又或者写一篇赞扬我们的文章。

    图片的读取与存储(二进制形式)

    类似与很多网站的注册功能以及相册展示功能 写了这个小列子 数据库 设计很简单就一个简单的表 两个字段 id和Image类型的content字段(用来存放图片用二进制读取的内容)还有一个存储过程(我喜欢用存储过程)数据库...

    手写模拟器v1.0 v2.0 v2.2

    手写模拟器v1.0 v2.0 v2.2三个版本 手写模拟器是一种软件工具,可以将文本内容转换为类似手写的文字图片...这种技术的应用不仅满足了人们对于个性化和艺术化的需求,同时也提供了一种新颖的方式来处理和展示文本内容。

    C# Winform自写控件

    这是我自己写的三个控件,以图片的方式展示,像android桌面使用方法一样,滑动进行翻页或移动,点击触发托管事件,支持加速度,移动方向可以设置成上下或者左右,支持翻页和滚动,还可以自动播放,不提供使用代码(避免懒人...

    产品图片放大效果制作视频教程

    很多电商平台在展示产品图片的时候,都有一个放大镜的效果,放大产品图片看产品的细节。...如何自己写一个放大镜效果呢,其实并不难,只要照着我们这套教程教的方法去做,就能实现。学习过程中遇到问题加QQ:1416759661

    ios-AJPhotoPicker 基于AssetsLibrary的照片选取器.zip

    网上也有一些做的很不错的类似控件,而大多数实现过于复杂不方便自己定制,在试用了几款后决定自己写这个控件;目前已经添加了几个自己需要的功能,同时控件在集成使用时也相对简单,几行代码 委托就可以了。在布局...

    产品图片放大效果制作视频教程-02

    很多电商平台在展示产品图片的时候,都有一个放大镜的效果,放大产品图片看产品的细节。...如何自己写一个放大镜效果呢,其实并不难,只要照着我们这套教程教的方法去做,就能实现。学习过程中遇到问题加QQ:1416759661

    【JavaScript源代码】vue卡片式点击切换图片组件使用详解.docx

     本文实例为大家分享了vue卡片式点击切换图片组件,供大家参考,具体内容如下 因为公司业务的问题,最近在写vue项目,有了一个卡片图片的点击的需求,自己又不想写动画效果,就偷个懒,采用vue是以数据驱动的原理...

    swift-GJImageCarouselView自己写的Banner轮播图

    自己写的Banner轮播图,自动循环,无限轮播。可以设置时间间隔、占位图。可以使用本地图片,也可以加载URL。

    CanimateCtrl_transparent.rar

    这是我自己写的基于MFC的小程序,是动态创建的!要注意的是建立了一全局变量CanimateCtr类型的指针,在单击按钮事件里初始化. 尤其要注意的是:由于是动态创建,所以先要创建一个CRect,它的初始化也很有意思:CRect rc...

    zxing.java源码解析-AndroidBadge:写一个android项目用于展示在开源项目中的徽章shields。方便大伙提升技能

    写一个 android 项目用于展示 在开源项目中的徽章 shields。方便大伙提升技能。 个人博客 打造一个高逼格的android开源项目——小白攻略 小引子 在平时的开发过程中,我们经常会查阅很多的资料,最常参考的是 github...

    中间图片放大,两边缩小,轮播效果

    中间图片放大,两边缩小,轮播效果,自己写的,没有其它的JS

    Unity3D UGUI特效之Image高斯模糊效果

    这几天研究了下模糊特效,看了很多文章,其原理就是拿取图片或屏幕数据,然后将周围的元素和目标位置的颜色值进行一个融合计算,然后自己写了一个小小的测试程序。 这个模糊也可以分成两种,一个是自身模糊,一个是...

    AS3.0+XML 相册 (自己写的)

    最近给公司网址做了一个相册展示的flash 拿出来跟大家一起分享下~ as3.0+XML 可自换图片 直接适用

Global site tag (gtag.js) - Google Analytics