How to obtain several directories in iPhone sandbox:
// Get the path of the sandbox home directoryNSString *homeDir = NSHomeDirectory(); // Get the Documents directory pathNSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *docDir = [paths objectAtIndex:0]; // Get the Caches directory pathNSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *cachesDir = [paths objectAtIndex:0]; // Get the tmp directory pathNSString *tmpDir = NSTemporaryDirectory(); // Get a picture resource() path in the current packageNSString *imagePath = [[NSBundle mainBundle] pathForResource:@"apple" ofType:@"png"]; UIImage *appleImage = [[UIImage alloc] initWithContentsOfFile:imagePath];
Three ways to save pictures to albums:
1. Use the UIImageWriteToSavedPhotosAlbum function to save the image to the album, such as:
- (void)loadImageFinished:(UIImage *)image { UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), (__bridge void *)self); } - (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo { NSLog(@"image = %@, error = %@, contextInfo = %@", image, error, contextInfo); }
The first parameter is the image object to be saved to the album
The second parameter is to save the target object called back after the completion
The third parameter is which method to call back to the target object after saving is completed. The method declaration should be as shown in the code:
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo;
After the fourth parameter is saved, it will be passed back to the contextInfo parameter of the callback method intact.
2. Use the ALAssetsLibrary class in the AssetsLibrary framework to implement it. The specific code is as follows:
- (void)loadImageFinished:(UIImage *)image { __block ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init]; [lib writeImageToSavedPhotosAlbum: metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { NSLog(@"assetURL = %@, error = %@", assetURL, error); lib = nil; }]; }
The writeImageToSavedPhotosAlbum:metadata:completionBlock: method of the ALAssetsLibrary class is implemented. The first parameter is an object of CGImageRef, indicating the image to be passed in. The second parameter is some properties of the image, which is not set here so nil is passed in. The last completionBlock is the callback after saving is completed. In this callback, you can get the saved image path and the error message when the save fails.
Notice:Importing is required when using this class. Moreover, this class needs to be used above iOS 4.0, but it is marked as an outdated method after iOS 9.0. The official suggests that PHPhotoLibrary is used instead, which is the third method mentioned below.
3. Use the PHPhotoLibrary class of the Photos framework to implement the save to album function. The code is as follows:
- (void)loadImageFinished:(UIImage *)image { [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ /Write pictures to album PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; } completionHandler:^(BOOL success, NSError * _Nullable error) { NSLog(@"success = %d, error = %@", success, error); }]; }
In this example, first call the performChanges:completionHandler: method of the PHPhotoLibrary class, and then in its changeBlock, pass an image object through the creationRequestForAssetFromImage: method of the PHAssetChangeRequest class to realize the function of saving to the album. Then the completionHandler will tell us whether the operation is successful.
Maybe someone needs to get the PHAsset object of the picture after saving the album to perform subsequent operations (I happened to encounter such a problem yesterday). So, here is an improvement to the above example. After creating a PHAssetChangeRequest, save the localIdentifier property of its placeholderForCreatedAsset property to an array, and wait for the operation to complete before searching for the image object you just added through this array.
Please see the chestnut below:
- (void)loadImageFinished:(UIImage *)image { NSMutableArray *imageIds = [NSMutableArray array]; [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ //Write the picture to the album PHAssetChangeRequest *req = [PHAssetChangeRequest creationRequestForAssetFromImage:image]; //Record the local identity and wait for completion to get the image object in the album [imageIds addObject:]; } completionHandler:^(BOOL success, NSError * _Nullable error) { NSLog(@"success = %d, error = %@", success, error); if (success) { //After successful, take the image object in the album __block PHAsset *imageAsset = nil; PHFetchResult *result = [PHAsset fetchAssetsWithLocalIdentifiers:imageIds options:nil]; [result enumerateObjectsUsingBlock:^(PHAsset * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) { imageAsset = obj; *stop = YES; }]; if (imageAsset) { //Load picture data [[PHImageManager defaultManager] requestImageDataForAsset:imageAsset options:nil resultHandler:^(NSData * _Nullable imageData, NSString * _Nullable dataUTI, UIImageOrientation orientation, NSDictionary * _Nullable info) { NSLog("imageData = %@", imageData); }]; } } }]; }
The above method of obtaining sandbox paths and saving pictures to the photo album is all the content I share with you. I hope you can give you a reference and I hope you can support me more.