Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CameraRoll] support fetching videos from the camera roll #774

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions Examples/UIExplorer/CameraRollView.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,16 @@ var propTypes = {
* imagesPerRow: Number of images to be shown in each row.
*/
imagesPerRow: React.PropTypes.number,

/**
* The asset type, one of 'Photos', 'Videos' or 'All'
*/
assetType: React.PropTypes.oneOf([
'Photos',
'Videos',
'All',
]),

};

var CameraRollView = React.createClass({
Expand All @@ -69,6 +79,7 @@ var CameraRollView = React.createClass({
groupTypes: 'SavedPhotos',
batchSize: 5,
imagesPerRow: 1,
assetType: 'Photos',
renderImage: function(asset) {
var imageSize = 150;
var imageStyle = [styles.image, {width: imageSize, height: imageSize}];
Expand All @@ -89,6 +100,7 @@ var CameraRollView = React.createClass({
assets: ([]: Array<Image>),
groupTypes: this.props.groupTypes,
lastCursor: (null : ?string),
assetType: this.props.assetType,
noMore: false,
loadingMore: false,
dataSource: ds,
Expand Down Expand Up @@ -124,7 +136,8 @@ var CameraRollView = React.createClass({

var fetchParams: Object = {
first: this.props.batchSize,
groupTypes: this.props.groupTypes
groupTypes: this.props.groupTypes,
assetType: this.props.assetType
};
if (this.state.lastCursor) {
fetchParams.after = this.state.lastCursor;
Expand Down Expand Up @@ -238,4 +251,4 @@ var styles = StyleSheet.create({
},
});

module.exports = CameraRollView;
module.exports = CameraRollView;
14 changes: 14 additions & 0 deletions Libraries/CameraRoll/CameraRoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,16 @@ var GROUP_TYPES_OPTIONS = [
'SavedPhotos', // default
];

var ASSET_TYPE_OPTIONS = [
'All',
'Videos',
'Photos', // default
];


// Flow treats Object and Array as disjoint types, currently.
deepFreezeAndThrowOnMutationInDev((GROUP_TYPES_OPTIONS: any));
deepFreezeAndThrowOnMutationInDev((ASSET_TYPE_OPTIONS: any));

/**
* Shape of the param arg for the `getPhotos` function.
Expand Down Expand Up @@ -58,6 +66,11 @@ var getPhotosParamChecker = createStrictShapeTypeChecker({
* titles.
*/
groupName: ReactPropTypes.string,

/**
* Specifies filter on asset type
*/
assetType: ReactPropTypes.oneOf(ASSET_TYPE_OPTIONS),
});

/**
Expand Down Expand Up @@ -152,5 +165,6 @@ class CameraRoll {
}

CameraRoll.GroupTypesOptions = GROUP_TYPES_OPTIONS;
CameraRoll.AssetTypeOptions = ASSET_TYPE_OPTIONS;

module.exports = CameraRoll;
14 changes: 12 additions & 2 deletions Libraries/Image/RCTCameraRollManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ - (void)callCallback:(RCTResponseSenderBlock)callback withAssets:(NSArray *)asse
NSString *afterCursor = params[@"after"];
NSString *groupTypesStr = params[@"groupTypes"];
NSString *groupName = params[@"groupName"];
NSString *assetType = params[@"assetType"];
ALAssetsGroupType groupTypes;

if ([groupTypesStr isEqualToString:@"Album"]) {
groupTypes = ALAssetsGroupAlbum;
} else if ([groupTypesStr isEqualToString:@"All"]) {
Expand All @@ -85,15 +87,23 @@ - (void)callCallback:(RCTResponseSenderBlock)callback withAssets:(NSArray *)asse
} else {
groupTypes = ALAssetsGroupSavedPhotos;
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure to remove the trailing whitespace.

BOOL __block foundAfter = NO;
BOOL __block hasNextPage = NO;
BOOL __block calledCallback = NO;
NSMutableArray *assets = [[NSMutableArray alloc] init];

[[RCTImageLoader assetsLibrary] enumerateGroupsWithTypes:groupTypes usingBlock:^(ALAssetsGroup *group, BOOL *stopGroups) {
if (group && (groupName == nil || [groupName isEqualToString:[group valueForProperty:ALAssetsGroupPropertyName]])) {
[group setAssetsFilter:ALAssetsFilter.allPhotos];

if (assetType == nil || [assetType isEqualToString:@"Photos"]) {
[group setAssetsFilter:ALAssetsFilter.allPhotos];
} else if ([assetType isEqualToString:@"Videos"]) {
[group setAssetsFilter:ALAssetsFilter.allVideos];
} else if ([assetType isEqualToString:@"All"]) {
[group setAssetsFilter:ALAssetsFilter.allAssets];
}

[group enumerateAssetsWithOptions:NSEnumerationReverse usingBlock:^(ALAsset *result, NSUInteger index, BOOL *stopAssets) {
if (result) {
NSString *uri = [(NSURL *)[result valueForProperty:ALAssetPropertyAssetURL] absoluteString];
Expand Down