15.6. Retrieving Photos and Videos from the Photo Library
Problem
You want users to be able to pick a photo or a video from their photo library and use it in your application.
Solution
Use the UIImagePickerControllerSourceTypePhotoLibrary
value for the source type of your UIImagePickerController
and the kUTTypeImage
or kUTTypeMovie
value, or both, for the
media type, like so:
-
(
BOOL
)
isPhotoLibraryAvailable
{
return
[
UIImagePickerController
isSourceTypeAvailable
:
UIImagePickerControllerSourceTypePhotoLibrary
];
}
-
(
BOOL
)
canUserPickVideosFromPhotoLibrary
{
return
[
self
cameraSupportsMedia:
(
__bridge
NSString
*
)
kUTTypeMovie
sourceType:
UIImagePickerControllerSourceTypePhotoLibrary
];
}
-
(
BOOL
)
canUserPickPhotosFromPhotoLibrary
{
return
[
self
cameraSupportsMedia:
(
__bridge
NSString
*
)
kUTTypeImage
sourceType:
UIImagePickerControllerSourceTypePhotoLibrary
];
}
-
(
void
)
viewDidAppear:
(
BOOL
)
animated
{
[
super
viewDidAppear
:
animated
];
static
BOOL
beenHereBefore
=
NO
;
if
(
beenHereBefore
){
/* Only display the picker once as the viewDidAppear: method gets
called whenever the view of our view controller gets displayed */
return
;
}
else
{
beenHereBefore
=
YES
;
}
if
([
self
isPhotoLibraryAvailable
]){
UIImagePickerController
*
controller
=
[[
UIImagePickerController
alloc
]
init
];
controller
.
sourceType
=
UIImagePickerControllerSourceTypePhotoLibrary
;
NSMutableArray
*
mediaTypes
=
[[
NSMutableArray
alloc
]
init
];
if
([
self
canUserPickPhotosFromPhotoLibrary
]){
[
mediaTypes
addObject
:
(
__bridge
NSString
*
)
kUTTypeImage ...
Get iOS 7 Programming Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.