use*_*751 6 objective-c uiimagepickercontroller ios
在我的应用程序中,有一个选择照片按钮,当点击按钮时,它应该拍摄 iPhone 照片并只显示图像而不显示视频。使用我的代码,它显示图像和视频。只显示图像的方法是什么.寻求帮助,感谢advanace。下面是我的代码:
#import <AssetsLibrary/AssetsLibrary.h>
-(void)choosePhotoFromExistingImages
{
if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary])
{
controller = [[UIImagePickerController alloc] init];
controller.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
controller.allowsEditing = NO;
controller.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypePhotoLibrary];
controller.delegate = self;
[self.navigationController presentViewController: controller animated: YES completion: nil];
}
Run Code Online (Sandbox Code Playgroud)
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
if (picker==controller){
[self.navigationController dismissViewControllerAnimated: YES completion: nil];
UIImage *image1 = [info valueForKey: UIImagePickerControllerOriginalImage];
CGFloat compression = 0.9f;
CGFloat maxCompression = 0.1f;
int maxFileSize = 250*1024;
NSData *imageData = UIImageJPEGRepresentation(image1, compression);
while ([imageData length] > maxFileSize && compression > maxCompression)
{
compression -= 0.1;
imageData = UIImageJPEGRepresentation(image1, compression);
}
imgVwProfile.image=image1;
// get the ref url
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
// define the block to call when we get the asset based on the url (below)
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset)
{
// ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
// strProileImg=[imageRep filename];
ALAssetRepresentation *imageRep=[imageAsset defaultRepresentation];
strProfileImg = [imageRep filename];
NSLog(@"%@",strProfileImg);
};
// get the asset library and fetch the asset based on the ref url (pass in block above)
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
}
}
Run Code Online (Sandbox Code Playgroud)
您需要使用mediaTypes属性:
controller.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];
Run Code Online (Sandbox Code Playgroud)
参考:
媒体类型
一个数组,指示媒体选择器控制器要访问的媒体类型。
@property (nonatomic, copy) NSArray *mediaTypes讨论
根据您分配给此属性的媒体类型,选择器会显示一个用于静止图像或电影的专用界面,或一个让用户选择选择器界面的选择控件。在设置此属性之前,请通过调用
availableMediaTypesForSourceType:类方法检查哪些媒体类型可用。如果将此属性设置为空数组,或设置为当前源没有任何媒体类型可用的数组,则系统会抛出异常。
捕获媒体时,此属性的值决定要显示的相机界面。浏览保存的媒体时,此属性决定了界面中显示的媒体类型。
默认情况下,此属性设置为 single value
kUTTypeImage,它指定捕获媒体时的静态相机界面,并指定在浏览保存的媒体时,媒体选择器中仅应显示静态图像。要指定电影捕获界面,或指示浏览保存的媒体时只应显示电影,请kUTTypeMovie在如下语句中使用标识符:
myImagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];要为源指定所有可用的媒体类型,请使用如下语句:
myImagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];
尝试通过以下方式替换设置 mediaTypes 的行:
controller.mediaTypes = @[kUTTypeImage];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2822 次 |
| 最近记录: |