ITeye博客

自己写的一个图片展示

wanglei0119 · 2010-10-13 15:49 · iPhone · 移动开发 · Markdown

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)

暂无评论。

评论功能维护中,当前仅展示历史评论。