如何在iPhone应用程序中点击选择照片按钮时仅显示图像而不显示视频

时间:2014-07-12 11:03:51

标签: ios objective-c uiimagepickercontroller

在我的应用程序中,选择照片按钮就在那里,当按下按钮时,它应该拍摄iPhone照片并仅显示图像而不显示视频。我的代码显示图像和视频。可能是通往只显示images.looking for help,谢谢advanace.Below是我的代码:

#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];
}

}

-(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];
}


}

8 个答案:

答案 0 :(得分:4)

您需要使用mediaTypes属性:

controller.mediaTypes = [[NSArray alloc] initWithObjects:(NSString *)kUTTypeImage, nil];

<强>参考:

  

<强> mediaTypes

     

一个数组,指示媒体选择器要访问的媒体类型   控制器。

     

@property (nonatomic, copy) NSArray *mediaTypes

     

<强>讨论

     

根据您为此属性指定的媒体类型,选择器   显示静止图像或电影的专用界面,或a   选择控件,允许用户选择选择器界面。   在设置此属性之前,请检查可用的媒体类型   调用availableMediaTypesForSourceType:类方法。

     

如果将此属性设置为空数组或其中的数组   没有任何媒体类型可用于当前源,   系统抛出异常。

     

捕获媒体时,此属性的值决定了摄像机   界面显示。浏览已保存的媒体时,此属性   确定界面中显示的媒体类型。

     

默认情况下,此属性设置为单个值kUTTypeImage,   在捕获媒体时指定静态相机界面,和   指定只应在媒体中显示静止图像   浏览保存的媒体时的选择器。指定电影捕捉   界面,或表示只有电影才能显示   浏览已保存的媒体,在语句中使用kUTTypeMovie标识符   像这样:

     

myImagePickerController.mediaTypes = [[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];

     

要为源指定所有可用的媒体类型,请使用语句   像这样:

     

myImagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

答案 1 :(得分:2)

默认情况下,mediatypes数组属性设置为单值kUTTypeImage,它指定捕获媒体时的静态相机界面,并指定在浏览已保存的媒体时,只有静止图像才会显示在媒体选择器中。

swift 3

    import MobileCoreServices
    imagePicker.mediaTypes = [kUTTypeImage as String] // this is the default value. 
    // you don't have to set it.

要为源指定所有可用的媒体类型,请使用如下语句:

    imagePicker.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!

注意:如果您需要设置媒体类型,请导入 MobileCoreServices

答案 2 :(得分:1)

尝试通过以下方式替换设置mediaTypes的行:

controller.mediaTypes = @[kUTTypeImage];

答案 3 :(得分:1)

在斯威夫特:

imagePicker.mediaTypes = [kUTTypeImage as String]

答案 4 :(得分:1)

雨燕4

// Make sure you need to import MobileCoreServices
import MobileCoreServices

pickerView.sourceType = .savedPhotosAlbum
pickerView.mediaTypes = [kUTTypeImage as String]

答案 5 :(得分:0)

试试这个:

  #import <MobileCoreServices/MobileCoreServices.h>
   controller.mediaTypes =[[NSArray alloc] initWithObjects: (NSString *)    kUTTypeImage, nil];

答案 6 :(得分:0)

在Swift 3.0中,默认情况下,您只会显示图片,而不会显示视频。因此,无需实施任何内容。但如果您想查看照片库中存在的所有类型的媒体,请使用以下代码:

let pick = UIImagePickerController()
pick.mediaTypes = UIImagePickerController.availableMediaTypes(for: .photoLibrary)!

答案 7 :(得分:-1)

试试这个:

controller.mediaTypes = @[kUTTypeImage];