iOS 保存图片和视频

保存图片

使用UIImageWriteToSavedPhotosAlbum方法保存图片到相册

- (void)saveImage:(UIImage *)image {
    UIImageWriteToSavedPhotosAlbum(image, self,
           @selector(UIImageWriteToSavedPhotosAlbum_image:didFinishSavingWithError:contextInfo:), nil);
}

- (void)UIImageWriteToSavedPhotosAlbum_image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        NSLog(@"UIImageWriteToSavedPhotosAlbum_image %@", error);
    } else {
        NSLog(@"UIImageWriteToSavedPhotosAlbum_image success");
    }
}

使用ALAssetsLibrary框架保存图片到相册

- (void)saveAssetLibraryImage:(UIImage *)image {
    ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
    [lib writeImageToSavedPhotosAlbum:image.CGImage metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error) {
            NSLog(@"saveAssetLibraryImage %@", error);
        } else {
            NSLog(@"saveAssetLibraryImage success");
        }
    }];
}

使用PHPhoto框架保存图片到相册

[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
        [PHAssetChangeRequest creationRequestForAssetFromImage:image];
    } completionHandler:^(BOOL success, NSError * _Nullable error) {
        if (error) {
            NSLog(@"savePhotoLibraryImage %@", error);
        } else {
            NSLog(@"savePhotoLibraryImage success");
        }
    }];

保存视频

使用UISaveVideoAtPathToSavedPhotosAlbum方法保存图片到相册

- (void)saveVideo:(NSURL *)mediaURL {
    UISaveVideoAtPathToSavedPhotosAlbum([mediaURL path], self,
            @selector(UISaveVideoAtPathToSavedPhotosAlbum_videoPath:didFinishSavingWithError:contextInfo:), nil);
}

- (void)UISaveVideoAtPathToSavedPhotosAlbum_videoPath:(NSString *)videoPath didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo{
    if (error) {
        NSLog(@"UISaveVideoAtPathToSavedPhotosAlbum_videoPath %@", error);
    } else {
        NSLog(@"UISaveVideoAtPathToSavedPhotosAlbum_videoPath success");
    }
}

使用ALAssetsLibrary框架保存视频到相册

- (void)saveAssetLibraryVideo:(NSURL *)mediaURL {
    ALAssetsLibrary *lib = [[ALAssetsLibrary alloc] init];
    [lib writeVideoAtPathToSavedPhotosAlbum:mediaURL completionBlock:^(NSURL *assetURL, NSError *error) {
        if (error) {
            NSLog(@"saveAssetLibraryVideo %@", error);
        } else {
            NSLog(@"saveAssetLibraryVideo success");
        }
    }];
}

使用PHPhoto框架保存视频到相册

- (void)savePhotoLibraryVideo:(NSURL *)mediaURL {
    [[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
            [PHAssetChangeRequest creationRequestForAssetFromVideoAtFileURL:mediaURL];
        } completionHandler:^(BOOL success, NSError * _Nullable error) {
            if (error) {
                NSLog(@"savePhotoLibraryVideo %@", error);
            } else {
                NSLog(@"savePhotoLibraryVideo success");
            }
        }];
}

权限

需要在Info.plist文件中,添加权限

在这里插入图片描述

图片压缩

iOS自带压缩方法UIImageJPEGRepresentation

- (NSData *)compressOriginalImage:(UIImage *)image toMaxDataSizeKBytes:(CGFloat)maxSize {
    NSData * data = UIImageJPEGRepresentation(image, 1.0);
    CGFloat dataKBytes = data.length;
    CGFloat maxQuality = 1.0f;
    while (dataKBytes > maxSize && maxQuality > 0.1f) {
        maxQuality = maxQuality - 0.1f;
        data = UIImageJPEGRepresentation(image, maxQuality);
        dataKBytes = data.length;
    }
    return data;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值