Various ways to read and save pictures from "IOS"

Source: Internet
Author: User
Tags uikit

I. reading a picture 1. Read from resource (Resource)
1 uiimage* image=[uiimage imagenamed:@ "1.jpg"

2. Read from the network "best use Egoimageview to get network pictures"
1 nsurl *url=[nsurl urlwithstring:@ "http://www.sinaimg.cn/qc/photo_auto/chezhan/2012/50/00/ 15/80046_950.jpg"];   2

3. Read locally from the phone
1 // read local picture non-  resource 2 nsstring *apath3=[nsstring stringWithFormat:@ "%@/documents/%@.jpg", Nshomedirectory (),@ "test"];   3 UIImage *imgfromurl3=[[UIImage alloc]initwithcontentsoffile:apath3];   4 uiimageview* Imageview3=[[uiimageview ALLOC]INITWITHIMAGE:IMGFROMURL3];  

 4. Get the image from the existing context
1 //add imageio.framework and #import2Cgimagesourceref Source =Cgimagesourcecreatewithurl ((cfurlref) URL, NULL); 3Cgimageref img= Cgimagesourcecreateimageatindex (source,0, NULL); 4Cgcontextref ctx=Uigraphicsgetcurrentcontext (); 5 cgcontextsavegstate (CTX); 6 //2 ways to TRANSFORMCTM7 //CGCONTEXTCONCATCTM (CTX, Cgaffinetransformmakescale (. 2,-0.2)); 8 //CGCONTEXTSCALECTM (ctx,1,-1); 9 //Note that coordinates should be reversed, using CTX as the source of the imageTenCgimageref capture=cgbitmapcontextcreateimage (CTX);  OneCgcontextdrawimage (CTX, CGRectMake ( the,0, the, the), [Image Cgimage]);  ACgcontextdrawimage (CTX, CGRectMake ( the, the, the, the), IMG);  -Cgimageref capture2=cgbitmapcontextcreateimage (CTX);

5. Use Quartz's cgimagesourceref to read pictures
1 cgimagesourceref Source = Cgimagesourcecreatewithurl ((cfurlref) URL, NULL);   2 cgimageref img= cgimagesourcecreateimageatindex (source,0, NULL);  

two. Save the picture1. Convert to NSData to save the picture (Imgfromurl is UIImage)
1 //Save Image 2 Get path can be2 //nsarray*paths=nssearchpathfordirectoriesindomains (NSDocumentDirectory, Nsuserdomainmask, YES); 3 //nsstring*documentsdirectory=[paths objectatindex:0]; 4 //nsstring*apath=[documentsdirectory stringbyappendingpathcomponent:[nsstring stringwithformat:@ "%@.jpg", @ "   Test "]); 5NSString *apath=[nsstring stringWithFormat:@"%@/documents/%@.jpg", Nshomedirectory (),@"Test"]; 6NSData *imgdata = uiimagejpegrepresentation (Imgfromurl,0); 7[Imgdata Writetofile:apath Atomically:yes];

When you use Uiimagewritetosavedphotosalbum to store images in a photo gallery, there are often thumbnails that can be seen but disappear from the original image.

With Uiimagewritetosavedphotosalbum (Imagesave, nil, nil, nil), Imagesave is the uiimage type, so it's saved in. And note that the picture should not be too large, so as to avoid the program crash "Save the picture in the Mobile album" Uiimagewritetosavedphotosalbum (Imgview.image,nil,nil,nil);


Save Image Extension: Sometimes the app may be blocked by mobile phone to access the album, resulting in the picture save failedSometimes we have the need to save images, such as using Uiimagepickercontroller to take pictures with a built-in camera on an iOS device, or sometimes we take advantage of Uikit's uigraphicsbeginimagecontext in the app, The Uigraphicsendimagecontext,uigraphicsgetimagefromcurrentimagecontext method creates an image that needs to be saved. The iOS Uikit framework provides the Uiimagewritetosavedphotosalbum method for saving images, which saves the image to the user's album, as described below:
1 voidUiimagewritetosavedphotosalbum (2UIImage *image,3    IDCompletiontarget,4 SEL Completionselector,5    void*ContextInfo6 );7 parameter Description:8 Image9 Uimage object with saved pictureTen Completiontarget One after saving the image to the album, call Completiontarget specified selector (optional) A Completionselector - The Completiontarget method corresponds to the selector, equivalent to the callback method, the following format should be satisfied -  the- (void) Image: (UIImage *) Image -Didfinishsavingwitherror: (Nserror *) Error -ContextInfo: (void*) ContextInfo;

CONTEXTINFO Specifies that incoming data can be selected in the callback.

When we need to asynchronously get the message of the image save result, we need to specify the Completiontarget object and its completionselector corresponding selector. Examples are as follows:

1- (void) Saveimagetophotos: (uiimage*) Savedimage2 {3 Uiimagewritetosavedphotosalbum (image, Self, @selector (image:didFinishSavingWithError:contextInfo:), NULL); 4 }5 //Specifying callback Methods6- (voidImage: (UIImage *) image didfinishsavingwitherror: (nserror *) Error ContextInfo: (void*) ContextInfo7 {   8NSString *msg =Nil; 9     if(Error! =NULL) {Tenmsg =@"failed to save picture" ; One}Else{ Amsg =@"Save Picture Success" ; -     } -Uialertview *alert = [[Uialertview alloc] Initwithtitle:@"Save picture result hint" the message:msg -                                                        Delegate: Self -Cancelbuttontitle:@"Determine" - Otherbuttontitles:nil]; + [alert show]; - } +  A //Invoke Example atUIImage *savedimage = [UIImage imagenamed:"Savedimage.png"]; -  -[Self saveimagetophotos:savedimage];
2. Use quartz cgimagedestinationref to output pictures, this way is not common, so do not introduce, detailed can see Apple document Quartz 2D Programming Guide three. Drawing Diagram (draw|painting)1.UIImageView method added to the UIView layer
1 uiimageview* imageview=[[Uiimageview alloc]initwithimage:image];   2 Imageview.frame=cgrectmake (00480);   3 [self addsubview:imageview];   4 [ImageView release];  

2.[img Drawatpoint] Series method
1 [image4 drawatpoint:cgpointmake (0)];    

3.CGContextDrawImage
1 cgcontextdrawimage (CTX, CGRectMake (0 )


4.CGLayer This is an apple-recommended offscreen drawing method that is better than bitmapcontext because it seems to use iphone hardware (Drawing-card) to accelerate
 1  cglayerref cg=cglayercreatewithcontext (CTX, Cgsizemake (320 , 480  ), NULL);  2  //  3  cgcontextref layercontext=< Span style= "color: #000000;"  >cglayergetcontext (CG);  4  cgcontextdrawimage (Layercontext, CGRectMake ( 160 , 230 , 160 , 230     5  cgcontextdrawlayeratpoint (CTX, Cgpointmake (  0 , 0 ), CG); 

5.CALayer of Contents
1 uiimage* image=[uiimage imagenamed:@ "1.jpg"];   2 calayer *ly=[calayer layer];   3 Ly.frame=cgrectmake (00460);   4 ly.contents=[Image cgimage];   5 [Self.layer addsublayer:ly];  

Four. Other1.CGImage and UIImage swaps so you can switch between uikit and quartz at any time, and choose the way you're familiar with the picture.
1 cgimage cgimage=[uiImage cgimage]; 2 uiimage* uiimage=[uiimage imagewithcgimage:cgimage];

Various ways to read and save pictures from "IOS"

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.