您的位置:首页 > 移动开发 > IOS开发

ios图片操作

2012-12-30 23:01 344 查看
先看UIImagePickerControllerDelegate的協定,主要是利用iOS內建的圖片選取控制器
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
取得照片後的處理,範例
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];//

UIImage *cmpImg = [appDelegate scaleImage:image toScale:kImageScaleRate];//縮圖

NSData *blobImage = UIImageJPEGRepresentation(cmpImg, kImageCompressRate);//圖片壓縮為NSData

[self dismissModalViewControllerAnimated:YES];

[self updateImage:blobImage withIndexPath:indexPath_];//更新圖片(自定義函數)

}
叫起圖片選取器
-(void)snapImage:(id)sender

{

UIImagePickerController *ipc = [[UIImagePickerController alloc]init];

ipc.sourceType = UIImagePickerControllerSourceTypeCamera;//圖片來源

ipc.videoQuality = UIImagePickerControllerQualityTypeLow;

ipc.delegate = self;

ipc.allowsEditing = NO;

[self presentModalViewController:ipc animated:YES];

}
縮圖函數(自定義)
- (UIImage *)scaleImage:(UIImage *)image toScale:(float)scaleSize

{

UIGraphicsBeginImageContext(CGSizeMake(image.size.width*scaleSize,image.size.height*scaleSize));

[image drawInRect:CGRectMake(0, 0,
image.size.width * scaleSize, image.size.height *scaleSize)];

UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return scaledImage;

}
儲存至資料庫※圖片傳入時已是NSData
-(void)updateImage:(NSData*)image withIndexPath:(NSIndexPath*)indexPath

{

NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];

[dateformat setDateFormat:@"YYYY-MM-dd HH:mm:ss"];

NSDate* spentDate = [(SpentMoney*)[dailyContent objectAtIndex:indexPath.row] spentDate];

NSString *date = [NSString stringWithString:[dateformat stringFromDate:
spentDate]];

//圖片傳入的時候,已經是NSData了,所以只要單純寫入即可

BOOL sucess = [fmdb executeUpdate:@"update
spentMoney set contentImage =? where spentDate=?",image, date];

if (!sucess) {

[appDelegate showMessageWith:@"fail
to insert image" andMessage:nil];

}

dateformat = nil;

[self fetchData];

}
自資料庫讀取,範例是tableView的Cell資料呈現的內容,看紅色字部份
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

if (cell==nil) {

cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];

}

cell.accessoryType = UITableViewCellAccessoryDetailDisclosureButton;

SpentMoney *spentMoney = [dailyContent objectAtIndex:indexPath.row];

NSDateFormatter *dateformat = [[NSDateFormatter alloc]init];

dateformat.dateFormat = @"HH:mm:ss";

cell.textLabel.text = [dateformat stringFromDate:
spentMoney.spentDate];

cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@",spentMoney.money];

//圖片存至資料庫時是用NSData,讀取也只要用imageWithData把圖片讀取出來

cell.imageView.image = [UIImage imageWithData:spentMoney.contentImage];

return cell;

dateformat = nil;

}

//add another author's passage

使用UIImagePickerController打开图片库和相机选择图片修改头像的主要方法如下,

声明:这个是iphone版本的,ipad版本的使用这个不行,因为iPad要用UIPopover才可以。

效果图:



[html] view
plaincopy

- (void)viewDidLoad

{

[super viewDidLoad];

//获取Documents文件夹目录

NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentPath = [path objectAtIndex:0];

//指定新建文件夹路径

NSString *imageDocPath = [documentPath stringByAppendingPathComponent:@"ImageFile"];

//创建ImageFile文件夹

[[NSFileManager defaultManager] createDirectoryAtPath:imageDocPath withIntermediateDirectories:YES attributes:nil error:nil];

//保存图片的路径

self.imagePath = [imageDocPath stringByAppendingPathComponent:@"image.png"];

}

-(void)viewWillAppear:(BOOL)animated{

[super viewWillAppear:YES];

//根据图片路径载入图片

UIImage *image=[UIImage imageWithContentsOfFile:self.imagePath];

if (image == nil) {

//显示默认

[changeImg setBackgroundImage:[UIImage imageNamed:@"user_photo@2x.png"] forState:UIControlStateNormal];

}else {

//显示保存过的

[changeImg setBackgroundImage:image forState:UIControlStateNormal];

}

}

- (void)dealloc {

[imagePath release];

[changeImg release];

[super dealloc];

}

- (IBAction)changeImage:(id)sender {

UIActionSheet *myActionSheet = [[UIActionSheet alloc]

initWithTitle:nil

delegate:self

cancelButtonTitle:@"取消"

destructiveButtonTitle:nil

otherButtonTitles: @"从相册选择", @"拍照",nil];

[myActionSheet showInView:self.view];

[myActionSheet release];

}

-(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{

switch (buttonIndex) {

case 0:

//从相册选择

[self LocalPhoto];

break;

case 1:

//拍照

[self takePhoto];

break;

default:

break;

}

}

//从相册选择

-(void)LocalPhoto{

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

//资源类型为图片库

picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

picker.delegate = self;

//设置选择后的图片可被编辑

picker.allowsEditing = YES;

[self presentModalViewController:picker animated:YES];

[picker release];

}

//拍照

-(void)takePhoto{

//资源类型为照相机

UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;

//判断是否有相机

if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]){

UIImagePickerController *picker = [[UIImagePickerController alloc] init];

picker.delegate = self;

//设置拍照后的图片可被编辑

picker.allowsEditing = YES;

//资源类型为照相机

picker.sourceType = sourceType;

[self presentModalViewController:picker animated:YES];

[picker release];

}else {

NSLog(@"该设备无摄像头");

}

}

#pragma Delegate method UIImagePickerControllerDelegate

//图像选取器的委托方法,选完图片后回调该方法

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{

//当图片不为空时显示图片并保存图片

if (image != nil) {

//图片显示在界面上

[changeImg setBackgroundImage:image forState:UIControlStateNormal];

//以下是保存文件到沙盒路径下

//把图片转成NSData类型的数据来保存文件

NSData *data;

//判断图片是不是png格式的文件

if (UIImagePNGRepresentation(image)) {

//返回为png图像。

data = UIImagePNGRepresentation(image);

}else {

//返回为JPEG图像。

data = UIImageJPEGRepresentation(image, 1.0);

}

//保存

[[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];

}

//关闭相册界面

[picker dismissModalViewControllerAnimated:YES];

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: