iOS使用照片選取器(UIImagePickerControllerDelegate)後儲存至資料庫、圖片壓縮

iOS使用照片選取器(UIImagePickerControllerDelegate)後儲存至資料庫、圖片壓縮
2012 03 20 11 10 21
先看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(00, 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.rowspentDate];

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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值