I have an app that shares images and videos to different services. One being Instagram. Since Instagram does not support sending videos directly I researched this blog.
I was able to do something similar for videos with the new Photos framework:
AVExport to convert to a proper video format.
PHPHotoLibrary > create new album.
PHPHotoLibrary > ad converted asset to the PHLibrary
PHPHotoLibrary > Fetch the PHAsset to get a library URL.
Send the Library URL to Instagram. This process works great for videos. However, I am having a problem with images! For some reason, the image will save to the library, but they are NOT visible in Instagram.
Any ideas why the image that is in the PhotoLibrary is not viewable Instagram? Is there a proper way to convert the image before doing the PHLibrary creationRequeest?
Here is my main snippet for the image process..
- (void)saveImageAsset:(UIImage*)theImage toAblum:(NSString*)title andCompletion:(void (^)(NSURL* libraryURL))completion {
__block NSString *objIdentifier;
NSData *imgData = UIImagePNGRepresentation(theImage);
theImage = [UIImage imageWithData:imgData];
[[PHPhotoLibrary sharedPhotoLibrary] performChanges:^{
PHAssetCollection *assetCollection = [self albumWithTitle:title];
PHAssetChangeRequest* createAssetRequest =
[PHAssetChangeRequest creationRequestForAssetFromImage:theImage];
PHObjectPlaceholder *assetPlaceholder = createAssetRequest.placeholderForCreatedAsset;
PHAssetCollectionChangeRequest *albumChangeRequest =
[PHAssetCollectionChangeRequest changeRequestForAssetCollection:assetCollection];
[albumChangeRequest addAssets:@[ assetPlaceholder ]];
objIdentifier = assetPlaceholder.localIdentifier;
} completionHandler:^(BOOL success, NSError *error) {
if (!success) {
NSLog(@"performChanges Error %@", error);
} else NSLog(@"Write Image Good");
//NSLog(@"objIdentifier = %@", objIdentifier);
PHFetchResult *newFetch = [PHAsset fetchAssetsWithLocalIdentifiers:@[objIdentifier] options:nil];
PHAsset *lastImageAsset = [newFetch lastObject];
// NSLog(@"Image Fetch = %@ --lastImageAsset %@", newFetch, lastImageAsset);
PHImageRequestOptions* options = [[PHImageRequestOptions alloc] init];
options.synchronous = YES;
options.deliveryMode = PHImageRequestOptionsDeliveryModeFastFormat;
options.networkAccessAllowed = NO;
options.version = PHImageRequestOptionsVersionOriginal;
[[PHImageManager defaultManager] requestImageForAsset:(PHAsset *)lastImageAsset
targetSize:CGSizeMake(640,640)
contentMode:PHImageContentModeDefault
options:options
resultHandler:^(UIImage *result, NSDictionary *info) {
//NSLog(@"info = %@", info);
NSURL *finalImageURL = info[@"PHImageFileURLKey"];
NSLog(@"finalImageURL = %@", finalImageURL);
if (!finalImageURL) completion(nil);
completion(finalImageURL);
}];
}];
}