自己写的一个图片展示

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][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 ;
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值