ios - Saving an image displayed in a UIImageView -
ios - Saving an image displayed in a UIImageView -
i have array of images displayed in uicollectionview.
when cell in collection view pressed, image pushed view controller , displayed in uiimageview.
i want able press button , save image users photographic camera roll.
but i'm having problem doing so...
i think i'm on right lines code can't work together:
- (ibaction)onclicksavephoto:(id)sender{ uiimage *img = [uiimage imagenamed:@"which ever image beingness displayed in image view"]; uiimagewritetosavedphotosalbum(img, nil, nil, nil); } how can manipulate code allow user save image displayed in image view?
thanks in advance!
update:
found solution problem in post:
save image in uiimageview ipad photos library
how save image library:
you can utilize function:
uiimagewritetosavedphotosalbum(uiimage *image, id completiontarget, sel completionselector, void *contextinfo); you need completiontarget, completionselector , contextinfo if want notified when uiimage done saving, otherwise can pass in nil.
more info here
supposedly faster way save image library using uiimagewritetosavedphotosalbum: there`s much more fast uiimagewritetosavedphotosalbum way using ios 4.0+ avfoundation framework
alassetslibrary *library = [[alassetslibrary alloc] init]; [library writeimagetosavedphotosalbum:[image cgimage] orientation:(alassetorientation)[image imageorientation] completionblock:^(nsurl *asseturl, nserror *error){ if (error) { // todo: error handling } else { // todo: success handling } }]; //for non-arc projects //[library release]; get image of whatever in uiimageview screenshot:
ios 7 has new method allows draw view hierarchy current graphics context. can used uiimage fast.
this category method on uiview view uiimage:
- (uiimage *)takesnapshot { uigraphicsbeginimagecontextwithoptions(self.myimageview.bounds.size, no, [uiscreen mainscreen].scale); [self drawviewhierarchyinrect:self.myimageview.bounds afterscreenupdates:yes]; // old style [self.layer renderincontext:uigraphicsgetcurrentcontext()]; uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); homecoming image; } it considerably faster existing renderincontext: method.
reference : https://developer.apple.com/library/ios/qa/qa1817/_index.html
update swift: extension same:
extension uiview { func takesnapshot() -> uiimage { uigraphicsbeginimagecontextwithoptions(self.myimageview.bounds.size, false, uiscreen.mainscreen().scale); self.drawviewhierarchyinrect(self.myimageview.bounds, afterscreenupdates: true) // old style: self.layer.renderincontext(uigraphicsgetcurrentcontext()) allow image = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); homecoming image; } } ios objective-c uiimageview uiimage
Comments
Post a Comment