IPhone saves and reads image demo

Source: Internet
Author: User

Show image: Train of Thought: to use filemanager to save the image name in a folder of a directory in a variable array, and then use the loop to save the image to the Image array, and then display it.

Show Image -(Ibaction) showimage :( ID ) Sender
{

Nsfilemanager * filemanager = [nsfilemanager defaultmanager];
Nsarray * files = [nsarray arraywitharray: [filemanager urlsfordirectory: nsmusicdirectory indomains: nsuserdomainmask];
Nslog ( @" Files: % d " , [Files count]);
Nslog ( @" The files is % @ " , [Files objectatindex: 0 ]);
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0 ];
Bool isdir = no;
Nslog ( @" % D " , [Paths count]);

Nserror * error = nil;
Nsarray * filelist = [[nsarray alloc] init];
Nsmutablearray * filepath = [nsmutablearray arraywithcapacity: 100 ];
Nsmutablearray * imgarray = [nsmutablearray arraywithcapacity: 200 ];
// Nsarray * filepath = [[nsarray alloc] init];
Filelist = [filemanager contentsofdirectoryatpath: documentsdirectory error: & error];
For (Nsstring * File In Filelist ){
Nslog ( @" Filename is: % @ " , File );
[Filepath addobject: [documentsdirectory stringbyappendingpathcomponent: file];

}

For ( Int I = 0 ; I <[filelist count]; I ++)
{
Nsstring * filepathname = [filepath objectatindex: I];
If ([Filepathname hassuffix: @" PNG " ]) {
Uiimage * temp = [uiimage imagewithcontentsoffile: filepathname];
[Imgarray addobject: temp];
}
}

[Imgview1 setimage: [imgarray objectatindex: 0 ];
[Imgview2 setimage: [imgarray objectatindex: 1 ];
[Imgview3 setimage: [imgarray objectatindex: 2 ];
[Imgview4 setimage: [imgarray objectatindex: 3 ];
[Imgview5 setimage: [imgarray objectatindex: 4 ];
[Imgview6 setimage: [imgarray objectatindex: 5 ];
[Imgview7 setimage: [imgarray objectatindex: 6 ];
[Imgview8 setimage: [imgarray objectatindex: 7 ];
[Imgview9 setimage: [imgarray objectatindex: 8 ];
Nsurl * url = [nsurl urlwithstring: @" Http://list.image.baidu.com/t/image_category/galleryimg/menstar/hk/luo_zhi_xiang.jpg " ];
Nsdata * Data = [nsdata datawithcontentsofurl: url];
Uiimage * IMG = [uiimage imagewithdata: Data];

Nslog ( @" Width: % F, heigh: % F " , IMG. Size. Width, IMG. Size. Height );
/* Test nshomedirectory
Nsstring * homedireary ary = [nshomedirectory () stringbyappendingpathcomponent: @ "A"];
Nslog (@ "Path: % @", homedireary ary ); */

/* Create a folder */
Nsstring * newpath = [documentsdirectory stringbyappendingpathcomponent: @" Test " ];

Bool isok = [filemanager createdirectoryatpath: newpath attributes: Nil];
Nsstring * ImagePath = [documentsdirectory stringbyappendingpathcomponent:@" Nodeoff.png " ];
Nslog ( @" ImagePath: % @ " , ImagePath );
Nsdata * imagedata = [nsdata datawithcontentsoffile: ImagePath];
Nsstring * newpathcopy = [nsstring stringwithformat: @" % @/Nodeoff.png " , Newpath];
Nslog (@" Newpathcopy; % @ " , Newpathcopy );

Bool issave = [filemanager createfileatpath: newpathcopy contents: imagedata attributes: Nil];
Nslog ( @" Issave: % @ " , Issave? @" Yes " : @" No " );
[Imgurl setimage: img];

 

 

Save image: Train of Thought. Save the image as nsdata and save it to a folder.

Save image -( Void ) Viewdidload {
[Super viewdidload];
Nsarray * paths = nssearchpathfordirectoriesindomains (nsdocumentdirectory, nsuserdomainmask, yes );
Nsstring * documentsdirectory = [paths objectatindex: 0 ];
Nsstring * savedimagepath1 = [documentsdirectory stringbyappendingpathcomponent: @" Left.png " ];
Nsstring * savedimagepath2 = [documentsdirectory stringbyappendingpathcomponent: @" Right.png " ];
Nsstring * savedimagepath3 = [documentsdirectory stringbyappendingpathcomponent: @" Right3.png " ];
Nsstring * savedimagepath4 = [documentsdirectory stringbyappendingpathcomponent: @" Up3.png " ];
Nsstring * savedimagepath5 = [documentsdirectory stringbyappendingpathcomponent: @" Nodeoff.png " ];
Nsstring * savedimagepath6 = [documentsdirectory stringbyappendingpathcomponent: @" Nodeon.png " ];
Nsstring * savedimagepath7 = [documentsdirectory stringbyappendingpathcomponent: @" Webcam002.png " ];
Nsstring * savedimagepath8 = [documentsdirectory stringbyappendingpathcomponent: @" Webcam003.png " ];
Nsstring * savedimagepath9 = [documentsdirectory stringbyappendingpathcomponent: @" Plus3.png " ];

// Also be. jpg or another
Uiimage * newimage1 = [uiimage imagenamed: @" Left.png " ];
Uiimage * newimage2 = [uiimage imagenamed: @" Right.png " ];
Uiimage * newimage3 = [uiimage imagenamed: @" Right3.png " ];
Uiimage * newimage4 = [uiimage imagenamed: @" Up3.png " ];
Uiimage * newimage5 = [uiimage imagenamed: @" Nodeoff.png " ];
Uiimage * newimage6 = [uiimage imagenamed: @" Nodeon.png " ];
Uiimage * newimage7 = [uiimage imagenamed: @" Webcam002.png " ];
Uiimage * newimage8 = [uiimage imagenamed: @" Webcam003.png " ];
Uiimage * newimage9 = [uiimage imagenamed: @" Plus3.png " ];

Nsdata * imagedata1 = uiimagepngrepresentation (newimage1 );
Nsdata * imagedata2 = uiimagepngrepresentation (newimage2 );
Nsdata * imagedata3 = uiimagepngrepresentation (newimage3 );
Nsdata * imagedata4 = uiimagepngrepresentation (newimage4 );
Nsdata * imagedata5 = uiimagepngrepresentation (newimage5 );
Nsdata * imagedata6 = uiimagepngrepresentation (newimage6 );
Nsdata * imagedata7 = uiimagepngrepresentation (newimage7 );
Nsdata * imagedata8 = uiimagepngrepresentation (newimage8 );
Nsdata * imagedata9 = uiimagepngrepresentation (newimage9 );

// Uiimagejpegrepresentation (image)
[Imagedata1 writetofile: savedimagepath1 atomically: Yes];
[Imagedata2 writetofile: savedimagepath2 atomically: Yes];
[Imagedata3 writetofile: savedimagepath3 atomically: Yes];
[Imagedata4 writetofile: savedimagepath4 atomically: Yes];
[Imagedata5 writetofile: savedimagepath5 atomically: Yes];
[Imagedata6 writetofile: savedimagepath6 atomically: Yes];
[Imagedata7 writetofile: savedimagepath7 atomically: Yes];
[Imagedata8 writetofile: savedimagepath8 atomically: Yes];
[Imagedata9 writetofile: savedimagepath9 atomically: Yes];

 

 

1: Save the image and read the saveimage of the image.

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.