[Go]ios: Batch import pictures and videos to the emulator's album

Source: Internet
Author: User

In iOS development we often use the simulator debugging, the simulator has a major advantage is the program startup block, most importantly, if there is no certificate, we can only debug on the simulator. When using the simulator debugging we may encounter the need to select pictures from the System album, especially for image processing class related programs, it is often used.

In the case of initialization the emulator is empty in the album, so to choose, we have to import some pictures first. The usual practice is to drag the image directly onto the simulator, and then the simulator will be opened with Safari, and this time we can press the picture will pop up saved to the System album menu, so a complete picture preservation process is completed. But this method has a clear drawback is that the efficiency is too low, imagine you want to do the picture processing must be tested a variety of sizes, various proportions of the picture, so that one import is undoubtedly a kind of destruction to the programmer.

So how can you import pictures in batches?

My first idea was to find the directory where the simulator was stored, and then navigate to the Photos folder and copy the pictures directly. Then open the Finder to find the simulator installation path, such as the 6.1 simulator path is:/users/maple/library/application support/iphone simulator/6.1/

We use the command line to enter this directory and find the path to the photo folder:/users/maple/library/application support/iphone Simulator/6.1/media/dcim, and then copy the photos in. Thought it was done, and the result restarted the simulator later found that the system album compared to the previous unchanged, think about it should only because we have only copied the picture, but did not update the system album corresponding database. If you can copy the picture at the same time, update the system album corresponding database, I guess should be able to fix, but the cost is relatively large, so had to give up.

Had to change another way, remember to do the previous project as if there is an API can save the picture to the System album, so find this api:uiimagewritetosavedphotosalbum. Think if I build a project, add the pictures to the project, and then in the program by traversing these pictures, one call Uiimagewritetosavedphotosalbum this method, so the picture is not all imported into the system album?

Say do, first write a simple function to try it, indeed can achieve the goal, and then continue to improve the idea, add the monitoring copy progress function, made a simple progress interface.

The key saving picture function fragment is as follows:

-(void) savemediatocameraroll{////traverse the main bundle to find out all image files Nsmutablearray *picarray =        [Nsmutablearray Arraywithcapacity:3];    Nsarray *jpgfiles = [[NSBundle mainbundle] pathsforresourcesoftype:@ "jpg" indirectory:nil];        [Picarray Addobjectsfromarray:jpgfiles];        Nsarray *pngarray = [[NSBundle mainbundle] pathsforresourcesoftype:@ "PNG" indirectory:nil];    Exclude launch image of this project nsmutablearray *picexcludedefault = [Nsmutablearray Arraywitharray:pngarray];        For (NSString *path in Pngarray) {Nsarray *pathcom = [path pathcomponents]; if ([pathcom containsobject:@ "[email protected]"] | | [Pathcom containsobject:@ "Default.png"] | | [Pathcom containsobject:@ "[email protected]"])        {[Picexcludedefault Removeobject:path];    }} Pngarray = Picexcludedefault;        [Picarray Addobjectsfromarray:pngarray]; Traverse the main bundle to find out all mov fiLes nsmutablearray *videoarray = [Nsmutablearray arraywithcapacity:3];    Nsarray *movs = [[NSBundle mainbundle] pathsforresourcesoftype:@ "mov" indirectory:nil];        [Videoarray Addobjectsfromarray:movs];        _mediaitemcount = Picarray.count + videoarray.count; if (delegate && [Delegate respondstoselector: @selector (savetoutilstartcopy:)]) {[Delegate SAVETOUTILSTARTC    Opy:_mediaitemcount]; }//Save pic to camera roll for (ID item in Picarray) {UIImage *img = [[UIImage alloc] Initwithcontents                Offile:item]; Note:save to camera roll was async, so the later item could copy complete than previous item UIIMAGEWRITETOSAVEDPH        Otosalbum (IMG, self, @selector (image:didFinishSavingWithError:contextInfo:), NULL);    [img Release]; }//Save video to the camera roll for (ID item in Videoarray) {if (Uivideoatpathiscompatiblewithsavedphotos Album (item)) {//Note:save to camera roll is asYnc, so the later item could copy complete than previous item Uisavevideoatpathtosavedphotosalbum (item, Self, @se        Lector (video:didFinishSavingWithError:contextInfo:), NULL); } else {[self updateprocesswitherror:[nserror errorwithdomain:@ ' copy video error ' Code:-1 Userinfo:nil]        ]; }    }}

Ignore interface update callback related logic, the main process of the program is as follows:

1) Find all the picture files in the current package, note that this block should be excluded from the project to bring pictures such as default.png, etc.;

2) Call the function saved to the system album for all the image files found.

After testing this program can realize the function of batch import picture to System album, so think further video can also be batch import? After the discovery found also has a api:uisavevideoatpathtosavedphotosalbum, and by the way added the bulk Import Video function, after testing can import normally.

Code for the entire project on GitHub, address: Https://github.com/smileEvday/SvSaveToCameraRoll

(From: http://www.cnblogs.com/smileEvday/archive/2013/04/16/SaveToCameraRoll.html)

[Go]ios: Batch import pictures and videos to the emulator's album

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.