iOS-???????????????(?????????)????????????

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了iOS-???????????????(?????????)????????????相关的知识,希望对你有一定的参考价值。

?????????auth   eal   ??????   onchange   nsbundle   bridge   ctp   selector   seq   

????????????????????????

1.?????????????????????????????????

2.?????????????????????????????????

3.??????????????????????????????????????????????????????????????????????????????

??????????????????

 

????????????????????????????????????

/*
 ??????????????????????????????????????????
 1.?????????????????????????????????
 1> C????????????UIImageWriteToSavedPhotosAlbum
 2> AssetsLibrary??????
 3> Photos??????
 
 2.?????????????????????????????????
 1> AssetsLibrary??????
 2> Photos??????
 
 3.???????????????????????????????????????????????????
 1> AssetsLibrary??????
 2> Photos??????
 */
- (IBAction)save:(UIButton *)sender {
    /*
     ????????????????????????
     1.?????????????????????????????????
     2.?????????????????????????????????
     3.??????????????????????????????????????????????????????????????????????????????
     */
    UIImageWriteToSavedPhotosAlbum(self.imageView.image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"???????????????"];
    } else {
        [SVProgressHUD showSuccessWithStatus:@"???????????????"];
    }
}
/*
//??????
 ???????????????-[NSInvocation setArgument:atIndex:]: index (2) out of bounds [-1, 1]
 ????????????????????????????????????????????????????????????????????????????????????????????????
 */

Photos???????????????????????????

1.Photos????????????

/*
??????Photos????????????
 1.PHAsset : ??????PHAsset?????????????????????????????????????????????????????????
 1> ??? : [PHAsset fetchAssets...]
 2> ????????? : PHAssetChangeRequest(?????????????????????????????????????????????)
 
 2.PHAssetCollection : ??????PHAssetCollection???????????????????????????
 1> ??? : [PHAssetCollection fetchAssetCollections...]
 2> ????????? : PHAssetCollectionChangeRequest(???????????????????????????????????????)
 
 3.??????????????????????????????????????????????????????????????????????????????block?????????
 -[phphotoLibrary performChanges:completionHandler:]
 -[PHPhotoLibrary performChangesAndWait:error:]
 */

/*
 Foundation???Core Foundation??????????????????????????????????????????NSString *???CFStringRef
 NSString *string = (__bridge NSString *)kCFBundleNameKey;
 CFStringRef string = (__bridge CFStringRef)@"test";
 
 ????????????????????????
 [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil]
 */

/*
 ???????????????This method can only be called from inside of -[PHPhotoLibrary performChanges:completionHandler:] or -[PHPhotoLibrary performChangesAndWait:error:]
 
// ???????????????????????? [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{ [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image]; } completionHandler:^(BOOL success, NSError * _Nullable error) { if (error) { [SVProgressHUD showErrorWithStatus:@"???????????????"]; } else { [SVProgressHUD showSuccessWithStatus:@"???????????????"]; } }]; // ???????????????????????? NSError *error = nil; [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{ [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image]; } error:&error]; if (error) { [SVProgressHUD showErrorWithStatus:@"???????????????"]; } else { [SVProgressHUD showSuccessWithStatus:@"???????????????"]; }
*/

2.Photos?????????????????????????????????

- (IBAction)save:(UIButton *)sender {
    /*
     ????????????????????????
     1.?????????????????????????????????
     2.?????????????????????????????????
     3.??????????????????????????????????????????????????????????????????????????????
     */
    NSError *error = nil;

    // ?????????????????????????????????
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image];
    } error:&error];

    if (error) {
        [SVProgressHUD showErrorWithStatus:@"???????????????"];
    } else {
        [SVProgressHUD showSuccessWithStatus:@"???????????????"];
    }
}

 

????????????????????????????????????

 Foundation???Core Foundation??????????????????????????????????????????NSString *???CFStringRef

 NSString *string = (__bridge NSString *)kCFBundleNameKey;

 CFStringRef string = (__bridge CFStringRef)@"test";

1.?????????????????????????????????

/** ??????App???????????????????????? */
- (PHAssetCollection *)createdCollection;

- (IBAction)save:(UIButton *)sender {
    /*
     ????????????????????????
     1.?????????????????????????????????
     2.?????????????????????????????????
     3.??????????????????????????????????????????????????????????????????????????????
     */
    [self createdCollection];
}

#pragma mark - ????????????App????????????????????????
- (PHAssetCollection *)createdCollection
{
    // ??????????????????
    NSString *title = [NSBundle mainBundle].infoDictionary[(__bridge NSString *)kCFBundleNameKey];
    
    // ??????????????????????????????
    PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    
    // ????????????App????????????????????????
    for (PHAssetCollection *collection in collections) {
        if ([collection.localizedTitle isEqualToString:title]) {
            return collection;
        }
    }
    
    /** ??????App?????????????????????????????????????????? **/
    // ?????????????????????????????????
    NSError *error = nil;
    __block NSString *createdCollectionID = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
createdCollectionID
= [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier; } error:&error]; if (error) { [SVProgressHUD showErrorWithStatus:@"?????????????????????"]; return nil; } // ????????????????????????????????????????????? return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionID] options:nil].firstObject; } /* Foundation???Core Foundation??????????????????????????????????????????NSString *???CFStringRef NSString *string = (__bridge NSString *)kCFBundleNameKey; CFStringRef string = (__bridge CFStringRef)@"test"; ???????????????????????? [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeSmartAlbum subtype:PHAssetCollectionSubtypeSmartAlbumUserLibrary options:nil] */

2.????????????

#pragma mark - ????????????App????????????????????????
// ?????????????????????????????????????????????
 return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionID] options:nil].firstObject;

3.??????????????????????????????

/** ??????App???????????????????????? */
- (PHAssetCollection *)createdCollection;
/** ???????????????????????????????????????????????? */
- (PHFetchResult<PHAsset *> *)createdAssets;

- (IBAction)save:(UIButton *)sender {
    PHAuthorizationStatus oldStatus = [PHPhotoLibrary authorizationStatus];
    
    // ???????????????????????? :
    // ???????????????????????????????????????????????????????????????????????????????????????????????????block
    // ????????????????????????????????????????????????block
    [PHPhotoLibrary requestAuthorization:^(PHAuthorizationStatus status) {
        dispatch_async(dispatch_get_main_queue(), ^{
            if (status == PHAuthorizationStatusDenied) { // ??????????????????App????????????
                if (oldStatus != PHAuthorizationStatusNotDetermined) {
                    HKLog(@"????????????????????????")
                }
            } else if (status == PHAuthorizationStatusAuthorized) { // ??????????????????App????????????
                [self saveImageIntoAlbum];
            } else if (status == PHAuthorizationStatusRestricted) { // ??????????????????
                [SVProgressHUD showErrorWithStatus:@"???????????????????????????????????????"];
            }
        });
    }];
}

/**
 *  ?????????????????????
 */
- (void)saveImageIntoAlbum
{
    // ????????????
    PHFetchResult<PHAsset *> *createdAssets = self.createdAssets;
    if (createdAssets == nil) {
        [SVProgressHUD showErrorWithStatus:@"?????????????????????"];
        return;
    }
    
    // ????????????
    PHAssetCollection *createdCollection = self.createdCollection;
    if (createdCollection == nil) {
        [SVProgressHUD showErrorWithStatus:@"?????????????????????????????????"];
        return;
    }
    
    // ???????????????????????????????????????????????????
    NSError *error = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
        [request insertAssets:createdAssets atIndexes:[NSIndexSet indexSetWithIndex:0]];
    } error:&error];
    
    // ???????????????
    if (error) {
        [SVProgressHUD showErrorWithStatus:@"?????????????????????"];
    } else {
        [SVProgressHUD showSuccessWithStatus:@"?????????????????????"];
    }
}


#pragma mark - ????????????App????????????????????????
- (PHAssetCollection *)createdCollection
{
    // ??????????????????
    NSString *title = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleDisplayName"];
    
    // ??????????????????????????????
    PHFetchResult<PHAssetCollection *> *collections = [PHAssetCollection fetchAssetCollectionsWithType:PHAssetCollectionTypeAlbum subtype:PHAssetCollectionSubtypeAlbumRegular options:nil];
    
    // ????????????App????????????????????????
    for (PHAssetCollection *collection in collections) {
        if ([collection.localizedTitle isEqualToString:title]) {
            return collection;
        }
    }
    
    /** ??????App?????????????????????????????????????????? **/
    // ?????????????????????????????????
    NSError *error = nil;
    __block NSString *createdCollectionID = nil;
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        createdCollectionID = [PHAssetCollectionChangeRequest creationRequestForAssetCollectionWithTitle:title].placeholderForCreatedAssetCollection.localIdentifier;
    } error:&error];
    
    if (error) return nil;
    
    // ?????????????????????????????????????????????
    return [PHAssetCollection fetchAssetCollectionsWithLocalIdentifiers:@[createdCollectionID] options:nil].firstObject;
}

- (PHFetchResult<PHAsset *> *)createdAssets
{
    NSError *error = nil;
    __block NSString *assetID = nil;
    
    // ?????????????????????????????????
    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
        assetID = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset.localIdentifier;
    } error:&error];
    
    if (error) return nil;
    
    // ???????????????????????????
    return [PHAsset fetchAssetsWithLocalIdentifiers:@[assetID] options:nil];
}

//???????????????????????????
//- (IBAction)save {
//    NSError *error = nil;
//
//    // ?????????????????????????????????
//    __block PHObjectPlaceholder *placeholder = nil;
//    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
//        placeholder = [PHAssetChangeRequest creationRequestForAssetFromImage:self.imageView.image].placeholderForCreatedAsset;
//    } error:&error];
//    if (error) {
//        [SVProgressHUD showErrorWithStatus:@"?????????????????????"];
//        return;
//    }
//
//    // ????????????
//    PHAssetCollection *createdCollection = self.createdCollection;
//    if (createdCollection == nil) {
//        [SVProgressHUD showErrorWithStatus:@"?????????????????????"];
//        return;
//    }
//
//    // ???????????????????????????????????????????????????
//    [[PHPhotoLibrary sharedPhotoLibrary] performChangesAndWait:^{
//        PHAssetCollectionChangeRequest *request = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:createdCollection];
//        [request insertAssets:@[placeholder] atIndexes:[NSIndexSet indexSetWithIndex:0]];
//    } error:&error];
//
//    // ???????????????
//    if (error) {
//        [SVProgressHUD showErrorWithStatus:@"?????????????????????"];
//    } else {
//        [SVProgressHUD showSuccessWithStatus:@"?????????????????????"];
//    }
//}

 

 

 

 

 

 



以上是关于iOS-???????????????(?????????)????????????的主要内容,如果未能解决你的问题,请参考以下文章

IO复用阻塞IO非阻塞IO同步IO异步IO

四种IO模型‘阻塞IO/非阻塞IO/信号驱动IO/异步IO‘

5种IO模型阻塞IO和非阻塞IO同步IO和异步IO

网络IO模型:同步IO和异步IO,阻塞IO和非阻塞IO

同步IO异步IO阻塞IO非阻塞IO之间的联系与区别

同步IO异步IO阻塞IO非阻塞IO之间的联系与区别