I want to get video from photoGallery when I click video button and which is displayed in same view. I have written this code, but don't get video into the imageview on same view.
- (IBAction)vedioClicked:(id)sender
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.allowsEditing = YES;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes =
[[NSArray alloc] initWithObjects: (NSString *) kUTTypeMovie, nil];
[self presentViewController:imagePicker animated:YES completion:nil];
}
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *editedImage = (UIImage *)[info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Compressing the image size
CGSize newSize = CGSizeMake(400, 400);
UIGraphicsBeginImageContext(newSize);
[editedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
inputImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image=inputImage;
// End the context
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(inputImage,0.2);//PNGRepresentation(imgView.image);
}
else if ([mediaType isEqualToString:#"public.movie"]){
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
}
self.imageView.image=inputImage;
[picker dismissViewControllerAnimated:YES completion:nil];
}
video is getting from photo library but its not display into imageView on same view so anybody help me to do this.Any help is more appreciated.
We can't directly display video in UIImageView. If we try to load directly it will display black screen only. To display video in UIImageView we need to load the first frame of video. For this need to import two frameworks.
1.AVFoundation
2.AssetsLibrary
Using these two we can display first frame of video into UIImageView as follows:
- (void) imagePickerController: (UIImagePickerController *) picker didFinishPickingMediaWithInfo: (NSDictionary *) info
{
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage])
{
UIImage *editedImage = (UIImage *)[info objectForKey:#"UIImagePickerControllerOriginalImage"];
// Compressing the image size
CGSize newSize = CGSizeMake(400, 400);
UIGraphicsBeginImageContext(newSize);
[editedImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
inputImage = UIGraphicsGetImageFromCurrentImageContext();
self.imageView.image=inputImage;
// End the context
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(inputImage,0.2);//PNGRepresentation(imgView.image);
}
else if ([mediaType isEqualToString:#"public.movie"])
{
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
AVAsset *avAsset = [AVURLAsset URLAssetWithURL:videoURL options:nil];
if ([[avAsset tracksWithMediaType:AVMediaTypeVideo] count] > 0)
{
AVAssetImageGenerator *imageGenerator =[AVAssetImageGenerator assetImageGeneratorWithAsset:avAsset];
Float64 durationSeconds = CMTimeGetSeconds([avAsset duration]);
CMTime midpoint = CMTimeMakeWithSeconds(durationSeconds/2.0, 600);
NSError *error;
CMTime actualTime;
CGImageRef halfWayImage = [imageGenerator copyCGImageAtTime:kCMTimeZero actualTime:&actualTime error:&error];
if (halfWayImage != NULL)
{
NSString *actualTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, actualTime));
NSString *requestedTimeString = (NSString *)CFBridgingRelease(CMTimeCopyDescription(NULL, midpoint));
NSLog(#"Got halfWayImage: Asked for %#, got %#", requestedTimeString, actualTimeString);
UIImage *img=[UIImage imageWithCGImage:halfWayImage];
self.imageView.image=img;
}
}
}
[picker dismissViewControllerAnimated:YES completion:nil];
}
Related
- (void)xuanquButtonClicked:(JohnButtonLong*)button
{
_picker = [[UIImagePickerController alloc] init];
if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
JLActionManager * manager = [JLActionManager sharedInstance];
[manager showAndHideHUDWithTitle:nil detailText:#"亲,您的相册无法打开" inView:self.view];
return;
}
_picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
_picker.allowsEditing = YES;
_picker.delegate = self;
[self presentViewController:_picker animated:YES completion:^{
}];
}
#pragma mark - UIIMagePickerController delegate Methods
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
// 如果没有选取 使用默认头像
_strHeadPic = #"";
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[picker dismissViewControllerAnimated:YES completion:^{
}];
// 选取了某个头像
UIImage * image = [info objectForKey:UIImagePickerControllerOriginalImage];
_img_touxiang.image = image;
//压缩图像
image = [self OriginImage:image scaleToSize:CGSizeMake(160, 160)];
NSData * img_data = UIImageJPEGRepresentation(image, 0.5);
//进行base64编码
NSData * data = [GTMBase64 encodeData:img_data];
_strHeadPic = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
// 压缩成指定大小
-(UIImage*) OriginImage:(UIImage *)image scaleToSize:(CGSize)size{
UIGraphicsBeginImageContext(size);
// 绘制改变大小的图片
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
// 从当前context中创建一个改变大小后的图片
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();// 是当前的context出对战
// 返回新的改变大小后的图片
return scaledImage;
}
UnitMyInfo * myInfo = GetMyInfo;
[_img_touxiang setImageWithURL:[NSURL URLWithString:BaseImgURL(myInfo.umi_URL)] placeholderImage:[UIImage imageNamed:#"default_headImg.jpg"]];
// First You have to get Url from Image then set url On SDWebImages.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editingInfo
{
[picker dismissModalViewControllerAnimated:YES];
NSURL *imagePath = [editingInfo objectForKey:#"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *localFilePath = [documentsDirectory stringByAppendingPathComponent:imageName];
NSLog(#"localFilePath.%#",localFilePath);
}
//Set Image On SDWebView.
[imageView setImageWithURL:[NSURL URLWithString:localFilePath] placeholderImage:[UIImage imageNamed:#"default_headImg.jpg"]];
Code that i used:
-(IBAction) getPhoto:(id) sender {
UIImagePickerController * picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
if((UIButton *) sender == choosePhotoBtn) {
picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
} else {
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
}
[self presentModalViewController:picker animated:YES];
}
following is the function to place image which was taken by using UIImagePickerControllerSourceTypeCamera in image view:
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
[picker dismissModalViewControllerAnimated:YES];
imageView.image = [info objectForKey:#"UIImagePickerControllerOriginalImage"];
}
Try this
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
img = [info valueForKey:UIImagePickerControllerEditedImage];
self.imageView.image = img;
NSData *imageData = UIImagePNGRepresentation(img);
This will work.
Convert like this,
NSData *imageData = UIImagePNGRepresentation(image);
If you want to convert back,
UIImage *image= [UIImage imageWithData:imagedata];
To get the NSData for image
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *imageData = UIImagePNGRepresentation(image);
I need to copy image from the device into the sandbox of the app! Say in Library/Cache folder! How do I do it? I searched in the internet i didnt find the solution. Please help!
EDIT:
#pragma mark - ZYQAssetPickerController Delegate
-(void)assetPickerController:(ZYQAssetPickerController *)picker didFinishPickingAssets:(NSArray *)assets{
ALAssetsLibrary *lib=[ALAssetsLibrary new];
for (int i=0; i<assets.count; i++) {
ALAsset *asset=assets[i];
FileOP *fileMgr=[[FileOP alloc]init];
NSString *baseDir=[fileMgr GetDocumentDirectory];
//STORING FILE INTO LOCAL
[lib assetForURL:asset.defaultRepresentation.url
resultBlock:^(ALAsset *asset){
ALAssetRepresentation *repr = [asset defaultRepresentation];
CGImageRef cgImg = [repr fullResolutionImage];
NSString *fname = repr.filename;
UIImage *img = [UIImage imageWithCGImage:cgImg];
NSData *data = UIImagePNGRepresentation(img);
[data writeToFile:[baseDir stringByAppendingPathComponent:fname]
atomically:YES];
//FOR LOCAL URL OF THE IMAGE
NSString *imageURL = [baseDir stringByAppendingPathComponent:fname];
NSLog(#"%# URL OF IMAGE ",imageURL);
[[ImageArray sharedImageArray]setImageUrl:imageURL atIndex:i];
//NSLog(#"%# is the shared array",[[ImageArray sharedImageArray] getImageUrlAtIndex:i];
}
failureBlock:^(NSError *error){
}];
}
NSLog(#"COPIED %lu FILE INTO LOCAL MEMORY",(unsigned long)assets.count);
//NEED TO STORE THE PATH OF THE SELECTED IMAGE FILES INTO SOME ARRAY HERE
}
You can do it like that :
In .h, implement the UIImagePickerControllerDelegate delegate :
#interface yourClass : NSObject <UIImagePickerControllerDelegate>
In .m, create a method fired when you click on a button. It will open your photo library and save the picked image into the cache folder :
- (IBAction)pickFromAlbum:(id)sender
{
UIImagePickerController *pickerController = [UIImagePickerController new];
pickerController.delegate = self;
pickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:pickerController animated:YES completion:nil];
}
Then implement the delegate method :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
// Get image into a variable
UIImage *takenImage = [info objectForKey:UIImagePickerControllerOriginalImage];
// Get the cache folder
NSArray *dirPaths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cacheDir = [dirPaths objectAtIndex:0];
// Save image into cache folder
NSData *data;
data = [NSData dataWithData:UIImageJPEGRepresentation(takenImage, 1.0f)]; // 1.0f = quality 100%
[data writeToFile:[cacheDir stringByAppendingPathComponent:#"fileName.jpeg"] atomically:YES];
// Hide the picker
[picker dismissViewControllerAnimated:YES completion:nil];
}
I selected image from photo library using UIImagePickerControllerReferenceURL, i got image path and assigned to imageView.image still not display that image, bellow is my code.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *refURL = [info valueForKey:UIImagePickerControllerReferenceURL];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset) {
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSString *homeDirectoryPath = NSHomeDirectory();
NSString *imagName = [imageRep filename];
NSString *imagePath = [homeDirectoryPath stringByAppendingString:imagName];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
imageView.image= [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imagePath]]];
UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(100,100,100,50)];
aView.backgroundColor=[UIColor lightGrayColor];
[aView addSubview:imageView];
[categoryDisplayScrollView addSubview:aView];
[self dismissViewControllerAnimated:YES completion:NULL];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
}
You can use UIImagePickerControllerOriginalImage for getting image.
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *imagepicked = [info objectForKey:UIImagePickerControllerOriginalImage];
ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *imageAsset) {
ALAssetRepresentation *imageRep = [imageAsset defaultRepresentation];
NSString *homeDirectoryPath = NSHomeDirectory();
NSString *imagName = [imageRep filename];
NSString *imagePath = [homeDirectoryPath stringByAppendingString:imagName];
imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,40,40)];
imageView.image= imagepicked;
UIView *aView=[[UIView alloc]initWithFrame:CGRectMake(100,100,100,50)];
aView.backgroundColor=[UIColor lightGrayColor];
[aView addSubview:imageView];
[categoryDisplayScrollView addSubview:aView];
[self dismissViewControllerAnimated:YES completion:NULL];
};
ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init];
[assetslibrary assetForURL:refURL resultBlock:resultblock failureBlock:nil];
}
Hope it helps you....!
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo (NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
[picker dismissViewControllerAnimated:YES completion:nil];
}
As mentioned in above code you can get UIImage from info dictionary with UIImagePickerControllerOriginalImage key.
And next is assign this UIImage to UIImageView.
This question already has answers here:
Getting image name of iphone photo library
(2 answers)
Closed 9 years ago.
I've searched how to get the name of the saved image takes from the camera, but i didn't find something simple.
this is my code :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
if( [picker sourceType] == UIImagePickerControllerSourceTypeCamera){
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
[library writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL *assetURL, NSError *error )
{
NSLog(#"IMAGE SAVED TO PHOTO ALBUM");
[library assetForURL:assetURL resultBlock:^(ALAsset *asset )
{
NSLog(#"we have our ALAsset!");
NSLog(#"%#", assetURL);
}
failureBlock:^(NSError *error )
{
NSLog(#"Error loading asset");
}];
}];
}
}
I you have the answer, i will happy to test it .
Thanks in advance.
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSURL *resourceURL;
[picker dismissViewControllerAnimated:YES completion:nil];
UIImage *image =[[UIImage alloc] init];
image =[info objectForKey:#"UIImagePickerControllerOriginalImage"];
NSURL *imagePath = [info objectForKey:#"UIImagePickerControllerReferenceURL"];
NSString *imageName = [imagePath lastPathComponent];
resourceURL = [info objectForKey:UIImagePickerControllerReferenceURL];
NSData *imageData;
NSString *extensionOFImage =[imageName substringFromIndex:[imageName rangeOfString:#"."].location+1 ];
if ([extensionOFImage isEqualToString:#"jpg"])
{
imageData = UIImagePNGRepresentation(image);
}
else
{
imageData = UIImageJPEGRepresentation(image, 1.0);
}
int imageSize=imageData.length/1024;
NSLog(#"imageSize--->%d", imageSize);
if (imageName!=nil) {
NSLog(#"imageName--->%#",imageName);
}
else
{
NSLog(#"no image name found");
}
}
If you are using ASSerts
-(void)assetPickerController:(WSAssetPickerController *)sender didFinishPickingMediaWithAssets:(NSArray *)assets
{
[self dismissViewControllerAnimated:YES completion:^{
if (assets.count < 1) return;
//self.pageControl.numberOfPages = assets.count;
int index = 0;
for (ALAsset *asset in assets) {
// NSString *imageName = [[asset defaultRepresentation] filename];
UIImage *image = [[UIImage alloc] initWithCGImage:asset.defaultRepresentation.fullScreenImage];
// (#"%#", [[asset defaultRepresentation] filename]);
NSData *imageData = UIImagePNGRepresentation(image);
int imageSize=imageData.length/1024;
(either)
NSURL *imagePath = [asset objectForKey:#"UIImagePickerControllerReferenceURL"];
// NSURL *imagePath = [NSURL URLWithString:[asset ob]];
NSString *imageName = [imagePath lastPathComponent];
(or)
NSLog(#"%#",[[asset defaultRepresentation] filename]);
index++;
}
}];
}
Try following code. I have used this in one of my previous project and its working for me :
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
_selectedImage = info[UIImagePickerControllerEditedImage];
self.photoView.image = _selectedImage;
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];
NSLog(#"[imageRep filename] : %#", [imageRep filename]);
};
// 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];
[picker dismissViewControllerAnimated:YES completion:NULL];
}