Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

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


}
like image 482
user2552751 Avatar asked Jul 12 '14 11:07

user2552751


People also ask

How can I see only Photos on my iPhone?

If you go to Settings>Photos & Camera and turn iCloud Photo Library off, then you will see the Camera Roll album under your Photos app. The Camera Roll only contains photos you have taken with your iPhone.

How do I view only videos on my iPhone?

Open the Photos app as you would to look at anything taken with the iOS Camera, then choose the “Albums” option. If it's not already displayed, select the back button to return to the primary “Albums” section. Locate and choose “Videos” to view only the videos taken with the iPhone camera.

How do I turn an iPhone video into a photo?

Just pause the video where you want to take the picture and quickly press the home and sleep buttons simultaneously. The screen will turn white and the shutter release sound will be heard ... The.


3 Answers

You need to use the mediaTypes property:

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

Reference:

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.

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. 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:

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

like image 135
Midhun MP Avatar answered Oct 19 '22 11:10

Midhun MP


Swift 4

// Make sure you need to import MobileCoreServices
import MobileCoreServices

pickerView.sourceType = .savedPhotosAlbum
pickerView.mediaTypes = [kUTTypeImage as String]
like image 40
Vivek Avatar answered Oct 19 '22 12:10

Vivek


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.

swift 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.

like image 3
abhimuralidharan Avatar answered Oct 19 '22 11:10

abhimuralidharan