打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
iOS使用照片選取器(UIImagePickerControllerDelegate)後儲存至資料庫、圖片壓縮
iOS使用照片選取器(UIImagePickerControllerDelegate)後儲存至資料庫、圖片壓縮
先看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;

}

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
详解iOS多图下载的缓存机制
UIImagePickerController
IOS 四种保存数据的方式
iOS7中容易被忽视的新特性
iOS开发
iOS的多线程
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服