iOS uses the photo picker (uiimagepickercontrollerdelegate) to save to the data library, the image compression

Source: Internet
Author: User
Tags dateformat
iOS uses the photo picker (uiimagepickercontrollerdelegate) to save to the data library, the image compression
First look at the Uiimagepickercontrollerdelegate protocol, mainly using iOS-built graphics to select the controller
-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary *) info
Get a picture of the processing, example

-(void) Imagepickercontroller: (Uiimagepickercontroller *) Picker Didfinishpickingmediawithinfo: (nsdictionary *) info

{

UIImage *image = [Info objectforkey:uiimagepickercontrolleroriginalimage];//

UIImage *cmpimg = [appdelegate scaleimage:image toscale:kimagescalerate];//thumbnail

NSData *blobimage = uiimagejpegrepresentation (cmpimg, kimagecompressrate);//Picture compressed to NSData

[Self dismissmodalviewcontrolleranimated:yes];

[Self updateimage:blobimage withindexpath:indexpath_];//update picture (custom function)

}

Call up the picture selector

-(void) SnapImage: (ID) sender

{

Uiimagepickercontroller *IPC = [[Uiimagepickercontroller alloc]init];

Ipc.sourcetype = uiimagepickercontrollersourcetypecamera;//Graphics source

ipc.videoquality = Uiimagepickercontrollerqualitytypelow;

Ipc.delegate = self;

ipc.allowsediting = NO;

[Self PRESENTMODALVIEWCONTROLLER:IPC animated:yes];

}

shrink function (definition)
-(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;

}

Save to Repository ※ Pictures are nsdata when they are uploaded
-(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]];

When the picture is introduced, it is already nsdata, so as long as you write it in simple

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];

}

From the data library, examples are the contents of TableView's cell data, see the Red character part

-(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];

When a picture is stored in a library, it is nsdata, read it, and just use the imagewithdata to read the picture out.

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

return cell;

DateFormat = nil;

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.