使用iOS 8中的新功能,如果您在应用程序中使用相机,它将要求访问相机的权限,然后当您尝试重新拍摄照片时,它会要求获得访问照片库的权限.下次启动应用程序时,我希望检查相机和照片库是否具有访问权限.
对于相机,我检查一下
if ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo] == AVAuthorizationStatusDenied)
{
// do something
}
Run Code Online (Sandbox Code Playgroud)
我正在为照片库寻找类似的东西.
我尝试使用以下代码将图像放入自定义相册:
PHAssetCollection *album = [self getMyAlbum];
UIImage *image = [self getMyImage];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
PHObjectPlaceholder * placeHolder = createAssetRequest.placeholderForCreatedAsset;
PHAssetCollectionChangeRequest *albumChangeRequest = [PHAssetCollectionChangeRequest changeRequestForAssetCollection:album];
if(placeHolder){
[albumChangeRequest addAssets:@[ placeHolder ]];
}
} completionHandler:^(BOOL success, NSError *error) {
//doesen't matter
}];
Run Code Online (Sandbox Code Playgroud)
因此,我在此行的用户日志中收到许多错误 createAssetRequest.placeholderForCreatedAsset
喜欢
1 CoreFoundation __exceptionPreprocess + 1245624
2 libobjc.A.dylib objc_exception_throw + 34136
3张照片__48- [PHChangeRequestHelper generateUUIDIfNecessary] _block_invoke + 116552
2 libdispatch.dylib _dispatch_semaphore_wait_slow + 79828
3张照片 - [PHChangeRequestHelper generateUUIDIfNecessary] + 115992
4张照片 - [PHAssetCreationRequest占位符ForCreatedAsset] + 244020
所以 …
我在两种情况下使用UIImagePickerController
在第一种情况下,当我从库中选择一个图像时,我可以轻松地在委托方法中获取URL:
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the URL
NSURL *url = [info valueForKey:UIImagePickerControllerReferenceURL];
...
}
Run Code Online (Sandbox Code Playgroud)
但是,当我拍摄新照片时,图片尚未出现在照片库中,并且还没有网址.所以,我首先需要在库中添加图像.但是,如何获取新资产的URL?
这是我在照片库中添加图像的代码
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get the image
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Add the image in the library
[[PHPhotoLibrary sharedPhotoLibrary]
performChanges:^
{
// Request creating an asset from the image.
PHAssetChangeRequest *createAssetRequest = [PHAssetChangeRequest creationRequestForAssetFromImage:image];
// Get the URL of the new asset here ?
...
}
completionHandler:^(BOOL success, NSError *error) …
Run Code Online (Sandbox Code Playgroud) 每次我尝试请求照片库的授权时,我的应用程序在模拟器中运行时都会崩溃.我在didFinishLaunchingWithOptions的appDelegate中使用以下代码:
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.authorized {
PHPhotoLibrary.requestAuthorization({ (status: PHAuthorizationStatus) in
})
}
Run Code Online (Sandbox Code Playgroud)
使用xcode 8 beta和swift 3.0.
使用新的照片框架,我能够在应用程序运行时检测照片库的更改,并使用photoLibraryDidChange检测后台.
但是当应用程序终止时(由用户或系统终止),如何在重新启动应用程序时检测到更改?
为了检测新照片,我能够保存上次更新的时间戳,并在重新启动时重新获取照片库.但我想不出有办法检测删除和更改的照片.
任何帮助表示赞赏!
谢谢!
我在为我正在处理的iOS应用程序实现相机View Controller时遇到问题.当按下一个按钮时,我有一个单独的相机对象来管理AVFoundation
事物的一面并捕获图像.如下所示,一旦UIImage
从相机捕获,我就会通过一个使用它的完成块.
下面的第一种方法是在相机上按下捕获按钮时触发的动作.首先,通过禁用预览图层上的连接暂停摄像机图层,然后捕获图像.然后,相机对象捕获UIImage
,此时我从视图中移除相机预览图层,而是UIImageView
在其位置添加捕获图像的子视图.
接下来,我想使用Photos框架将图像添加到我在Photos中创建的相册中.我可以毫无问题地创建相册,并且我已经验证了该PHAssetCollection
对象.我在第二种方法中使用的是正确的方法.
但是,出于某种原因,我无法将UIImage
I capture 添加到相册中.我尝试将我项目中的随机图像文件添加到相册中,操作成功完成.我还验证了addPhotoToSavedPhotos
通过使用NSLog
语句检查两种方法中的图像描述,成功传递了正确的图像.这让我相信图像不知何故出了问题,但是imageView成功地显示了它,所以我不确定它可能是什么.
如果有人对我可能会尝试的解决方案有任何想法,我将不胜感激.另外error.localizedDescription
从NSLog
第二种方法中的语句输出"The operation couldn't be completed
.(可可错误-1)."
- (IBAction)capturePhoto:(id)sender {
[self pauseCameraLayer];
[[eventCamera sharedInstance] captureStillUIImage:^(UIImage *image, NSError *error){
if(error)
{
NSLog(@"error capturing photo");
}
else
{
NSLog(@"%@",image.debugDescription);
}
[_captureButton setHidden:YES];
[_switchCameraButton setHidden:YES];
UIImageView *preview=[[UIImageView alloc] initWithImage:image];
[preview setAutoresizesSubviews:YES];
[preview setContentMode:UIViewContentModeScaleAspectFit];
[preview setTransform:CGAffineTransformMakeRotation(M_PI_2)];
preview.frame=_imageView.bounds;
[_imageView addSubview:preview];
[self addPhotoToSavedPhotos:[image copy]];
NSLog(@"1: %@",image.description);
}];
-(void)addPhotoToSavedPhotos:(UIImage*)photo
{ …
Run Code Online (Sandbox Code Playgroud) 我有一个奇怪的问题,将.mov
我的应用程序创建的文件从文档文件夹移动到相机胶卷.一点背景:
该应用程序制作时间推移电影.它专门用于具有1200万像素4032x3024传感器的设备.它在应用程序的文档文件夹中创建了电影.电影可以保存为4k或HD.它们也可以保存为整个传感器的4:3宽高比电影,或传感器的16:9裁剪.如果用户希望将电影存储在设备的相机胶卷中,则可以设置该选项.尝试将全尺寸电影(4032x3024)从应用程序的文档文件夹移动到相机胶卷时存在问题.我收到此错误:
错误域= NSCocoaErrorDomain代码= -1"(null)"
电影很好,它仍然坐在文档的文件夹中.它无法复制到相机胶卷.如果我通过相同的代码执行相同的操作与任何其他大小,没有问题.一个4:3高清(1440x1080)工作正常,16:9高清(1920x1080)工作正常,16:9 4k(3880x2160)工作正常.当我尝试移动它时,它只是4:3 4k(4032x3024)产生此错误.
这是移动的代码:
PHPhotoLibrary.shared().performChanges({
PHAssetChangeRequest.creationRequestForAssetFromVideo(atFileURL: cameraRollURL!)
Run Code Online (Sandbox Code Playgroud)
该URL正常,因为它可以很好地与其他大小一起使用.
我想要做的是将视频保存到PHPhotoLibrary,然后在上传到应用程序中的客户端远程服务器时将其删除(基本上,照片库充当临时存储,以便在任何事情都失败的情况下添加额外的安全层(I已经将我的视频保存在应用程序目录中).
问题:
问题是要工作,一切都必须在没有用户输入的情况下工作.您可以将视频写入照片库,如下所示:
func storeVideoToLibraryForUpload(upload : SMUpload) {
if PHPhotoLibrary.authorizationStatus() != PHAuthorizationStatus.Authorized {
// Don't write to library since this is disallowed by user
return
}
PHPhotoLibrary.sharedPhotoLibrary().performChanges({ () -> Void in
// Write asset
let assetRequest = PHAssetChangeRequest.creationRequestForAssetFromVideoAtFileURL(NSURL(fileURLWithPath: upload.nonsecureFilePath!)!)
let assetPlaceholder = assetRequest.placeholderForCreatedAsset
let localIdentifier = assetPlaceholder.localIdentifier
// Store local identifier for later use
upload.localAssetIdentifier = localIdentifier
}, completionHandler: { (success, error) -> Void in
....
})
}
Run Code Online (Sandbox Code Playgroud)
这完美无瑕地工作,我得到本地标识符,我存储它供以后使用..独角兽和彩虹.
现在,当我想在上传完成后立即删除该视频时,我会调用以下内容:
func removeVideoFromLibraryForUpload(upload : SMUpload) {
// Only proceed if there …
Run Code Online (Sandbox Code Playgroud) 但是我们现在如何确定设置了哪个照片库访问级别?
[PHPhotoLibrary authorizationStatus]
仅适用于"读写"权限检查.如果应用仅询问"仅添加照片"权限,则会保留该权限PHAuthorizationStatusNotDetermined
.如果用户将其从"读取和写入"更改为"仅添加照片",则会给出PHAuthorizationStatusDenied
.
那么,我如何判断我的应用是否有权执行"导出到相机胶卷"功能,这不需要读取权限?
我正在开发一个iOS应用程序,我需要像Instagram这样的画廊视图.我已经添加了图库视图,相机视图和视频视图,在拍摄图像后,它会保存到照片的自定义相册中.现在我想从自定义相册中检索这些图像并将其显示到Gallery的Collection视图.但是我在从自定义相册中检索这些图片时遇到困难.任何帮助将不胜感激.
编辑:我添加了用于创建自定义相册的PHPhotoLibrary,在此之前我添加了AssetsLibrary框架.
然后我创建了一个NSObject类(CustomAlbum),用于使用PHPhotoLibrary创建和管理自定义相册.
// CustomAlbum.h
#import <Foundation/Foundation.h>
#import <Photos/Photos.h>
@interface CustomAlbum : NSObject
//Creating album with given name
+(void)makeAlbumWithTitle:(NSString *)title onSuccess:(void(^)(NSString *AlbumId))onSuccess onError: (void(^)(NSError * error)) onError;
//Get the album by name
+(PHAssetCollection *)getMyAlbumWithName:(NSString*)AlbumName;
//Add a image
+(void)addNewAssetWithImage:(UIImage *)image toAlbum:(PHAssetCollection *)album onSuccess:(void(^)(NSString *ImageId))onSuccess onError: (void(^)(NSError * error)) onError;
//get the image using identifier
+ (void)getImageWithIdentifier:(NSString*)imageId onSuccess:(void(^)(UIImage *image))onSuccess onError: (void(^)(NSError * error)) onError;
@end
Run Code Online (Sandbox Code Playgroud)
// CustomAlbum.m
#import "CustomAlbum.h"
@implementation CustomAlbum
#pragma mark - PHPhoto
+(void)makeAlbumWithTitle:(NSString *)title onSuccess:(void(^)(NSString *AlbumId))onSuccess onError: (void(^)(NSError * …
Run Code Online (Sandbox Code Playgroud)