Friday, 15 August 2014

iphone - Limit the number of images saved in documents folder -



iphone - Limit the number of images saved in documents folder -

i want take 3 images , save in documents folder image_1, image_2, , image_3. want limit number of images 3 images, means 4th image saved new "image_1" replacing first image(old image_1), 5th becomes "image_2", , on. codes below save images image_1, image_2, image_3, image_4, ... how should limit number of images saved?

- (void) saveimage:(uiimage*)image { nsdata *imagedata = [nsdata datawithdata:uiimagejpegrepresentation(image, 0.9f)]; [imagedata writetofile:[self savepath] atomically:yes]; } - (nsstring*) savepath { int i=1; nsstring *path; { path = [nsstring stringwithformat: @"%@/documents/image_%d.jpg", nshomedirectory(), i++]; } while ([[nsfilemanager defaultmanager] fileexistsatpath:path]); homecoming path; }

static int i=1; // place below @implementation yourclassname - (void) saveimage:(uiimage*)image { nsdata *imagedata = [nsdata datawithdata:uiimagejpegrepresentation(image, 0.9f)]; nsfilemanager *filemgr = [nsfilemanager defaultmanager]; nsstring *path = [self savepath]; if([filemgr fileexistsatpath:path]) [filemgr removeitematpath:path error:null]; [imagedata writetofile:path atomically:yes]; } - (nsstring*) savepath { if(i==4) i=1; homecoming [nsstring stringwithformat: @"%@/documents/image_%d.jpg", nshomedirectory(), i++]; }

hope helps.

iphone

No comments:

Post a Comment