简体   繁体   English

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

[英]how to show only images without showing videos when choose photo button is tapped in iPhone app

In my app a choose photo button is there,when tapped on the button it should take to iPhone photos and show only images without showing videos present.With my code its showing images as well as videos.What could be the way to show only images.looking for help,thanks in advanace.Below is my code: 在我的应用程序中,选择照片按钮就在那里,当点击按钮时,它应该拍摄iPhone照片并仅显示图像而不显示视频。使用我的代码显示图像和视频。可能是仅显示图像的方式。寻求帮助,谢谢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];
}


}

You need to use the mediaTypes property: 您需要使用mediaTypes属性:

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

Reference: 参考:

mediaTypes mediaTypes

An array indicating the media types to be accessed by the media picker controller. 一个数组,指示媒体选择器控制器要访问的媒体类型。

@property (nonatomic, copy) NSArray *mediaTypes

Discussion 讨论

Depending on the media types you assign to this property, the picker displays a dedicated interface for still images or movies, or a selection control that lets the user choose the picker interface. 根据您为此属性指定的媒体类型,选取器会显示静态图像或影片的专用界面,或者允许用户选择选取器界面的选择控件。 Before setting this property, check which media types are available by calling the availableMediaTypesForSourceType: class method. 在设置此属性之前,请通过调用availableMediaTypesForSourceType: class方法检查可用的媒体类型。

If you set this property to an empty array, or to an array in which none of the media types is available for the current source, the system throws an exception. 如果将此属性设置为空数组,或者将当前源中没有任何媒体类型可用的数组设置为系统,则系统会引发异常。

When capturing media, the value of this property determines the camera interface to display. 捕获媒体时,此属性的值确定要显示的摄像头界面。 When browsing saved media, this property determines the types of media presented in the interface. 浏览已保存的媒体时,此属性确定界面中显示的媒体类型。

By default, this property is set to the single value kUTTypeImage , which designates the still camera interface when capturing media, and specifies that only still images should be displayed in the media picker when browsing saved media. 默认情况下,此属性设置为单值kUTTypeImage ,用于在捕获媒体时指定静态相机界面,并指定在浏览已保存的媒体时,只应在媒体选择器中显示静止图像。 To designate the movie capture interface, or to indicate that only movies should be displayed when browsing saved media, use the kUTTypeMovie identifier in a statement like this: 要指定电影捕获界面,或指示在浏览已保存的媒体时只显示电影,请在kUTTypeMovie语句中使用kUTTypeMovie标识符:

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

To designate all available media types for a source, use a statement like this: 要为源指定所有可用的媒体类型,请使用如下语句:

myImagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType: UIImagePickerControllerSourceTypeCamera];

By default, mediatypes array property is set to the single value kUTTypeImage, which designates the still camera interface when capturing media, and specifies that only still images should be displayed in the media picker when browsing saved media. 默认情况下,mediatypes数组属性设置为单值kUTTypeImage,它指定捕获媒体时的静态相机界面,并指定在浏览已保存的媒体时,只应在媒体选择器中显示静止图像。

swift 3 : 迅捷3

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

To designate all available media types for a source, use a statement like this: 要为源指定所有可用的媒体类型,请使用如下语句:

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

NB: import MobileCoreServices if you need to set the media types. 注意:如果需要设置媒体类型,请导入MobileCoreServices

Swift 4 斯威夫特4

// Make sure you need to import MobileCoreServices
import MobileCoreServices

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

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

controller.mediaTypes = @[kUTTypeImage];

在Swift中:

imagePicker.mediaTypes = [kUTTypeImage as String]

Try this : 尝试这个 :

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

In Swift 3.0, by default you will be shown images only, not videos. 在Swift 3.0中, 默认情况下,您只会显示图像,而不会显示视频。 So, no need to implement anything. 所以,不需要实现任何东西。 But if you want to see all types of media present in photo library, use following code: 但是,如果要查看照片库中存在的所有类型的媒体,请使用以下代码:

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

尝试这个:

controller.mediaTypes = @[kUTTypeImage];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM