Batch Import images and videos to the album of the simulator
Simulator debugging is often used in iOS development. One of the main advantages of simulators isProgramStart block. The most important thing is that if there is no certificate, we can only debug it on the simulator. When using the simulator for debugging, we may encounter the need to select images from the system album, especially when using image processing programs.
The album in the simulator is empty during initialization, so to select an image, we must first import some images. The usual practice is to drag the image directly to the simulator, and then the simulator will open it in safari. At this time, we will press the image for a long time and then the menu will pop up and save it to the system album, this completes the complete image storage process. However, one obvious disadvantage of this method is that the efficiency is too low. If you want to perform image processing, you must test images of various sizes and proportions, importing data one by one is undoubtedly a type of damage to programmers.
How can we import images in batches?
In my first thought, I found the directory where the simulator was stored, located under the photo folder, and copied the image directly. Then open the finder and find the simulator installation path. For example, the path of the 6.1 simulator is:/Users/maple/library/Application Support/iPhone simulator/6.1/
Run the command line to enter the Directory and find the path of the photo Folder: /Users/maple/library/Application Support/iPhone simulator/6.1/Media/dcimAnd then copy the photo. I thought it was a success. After restarting the simulator, I found that the system album was not changed. I think it should be because we only copied the image, but did not update the database corresponding to the system album. If you can update the database corresponding to the system album while copying images, I guess it can be done, but the cost is relatively high, so you have to give up.
I had to change the method. I remember when I was working on the previous project, there seemed to be an API that could save images to the system album. So I found thisAPI: uiimagewritetosavedphotosalbum. If I create a project, add the images to the project, traverse the images in the program, and callUiimagewritetosavedphotosalbumIn this way, won't all the images be imported into the system album?
After doing so, I first wrote a simple function and tried it. It can indeed achieve the goal. Then I continued to improve my thinking and added the function of monitoring copy progress, A simple progress interface is provided.
The key function snippets for saving images are 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: @" Default-568h@2x.png " ] | [Pathcom containsobject: @" Default.png " ] | [Pathcom containsobject: @" Default@2x.png " ]) {[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 Savetoutilstartcopy: _ mediaitemcount];} // Save PIC to camera roll For ( ID Item In Picarray) {uiimage * IMG = [[Uiimage alloc] initwithcontentsoffile: item]; // Note: Save to camera roll is async, so the later item may copy complete than previous item Uiimagewritetosavedphotosalbum (IMG, self, @ selector (image: didfinishsavingwitherror: contextinfo :), null); [img release];} // Save video to camera roll For ( ID Item In Videoarray ){ If (Uivideoatpathiscompatiblewithsavedphotosalbum (item )){ // Note: Save to camera roll is async, so the later item may copy complete than previous item Uisavevideoatpathtosavedphotosalbum (item, self, @ selector (Video: didfinishsavingwitherror: contextinfo :), null );} Else {[Self updateprocesswitherror: [nserror errorwithdomain: @" Copy video error " Code :- 1 Userinfo: Nil] ;}}
The main process of the program is as follows:
1. Find all the image files in the current program package, and remove the images that are provided by the project, such as default.png;
2) Call the function for saving all image files found to the system album.
After testing, this program can import images in batches to the system album. So I wonder if further videos can be imported in batches? You can also findAPI: uisavevideoatpathtosavedphotosalbumAnd then added the batch video import function. After testing, the video can be imported normally.
The entire projectCodeOn GitHub, address: https://github.com/smileEvday/SvSaveToCameraRoll
Note: Please indicate the source for reprinting! Welcome to join me in QQ 1592232964 to discuss common progress.