Open ambujshukla opened 9 years ago
TODO.zip // // ALAssetsLibrary category to handle a custom photo album // // Created by Mayank Jain on 16/09/14. // Copyright (c) 2014 cdnsol.com. All rights reserved. //
/this category is created to create Album with name "Smiley Lion" in phone's photo gallery and save all photo in this album/
@implementation ALAssetsLibrary(CustomPhotoAlbum)
-(void)saveImage:(UIImage)image toAlbum:(NSString)albumName withCompletionBlock:(SaveImageCompletion)completionBlock { //write the image data to the assets library (camera roll) [self writeImageToSavedPhotosAlbum:image.CGImage orientation:(ALAssetOrientation)image.imageOrientation completionBlock:^(NSURL* assetURL, NSError* error) {
//error handling
if (error!=nil) {
completionBlock(error);
return;
}
//add the asset to the custom photo album
[self addAssetURL: assetURL
toAlbum:albumName
withCompletionBlock:completionBlock];
}];
}
-(void)addAssetURL:(NSURL)assetURL toAlbum:(NSString)albumName withCompletionBlock:(SaveImageCompletion)completionBlock { __block BOOL albumWasFound = NO;
//search all photo albums in the library
[self enumerateGroupsWithTypes:ALAssetsGroupAlbum
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
//compare the names of the albums
if ([albumName compare: [group valueForProperty:ALAssetsGroupPropertyName]]==NSOrderedSame) {
//target album is found
albumWasFound = YES;
//get a hold of the photo's asset instance
[self assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//add photo to the target album
[group addAsset: asset];
//run the completion block
completionBlock(nil);
} failureBlock: completionBlock];
//album was found, bail out of the method
return;
}
if (group==nil && albumWasFound==NO) {
//photo albums are over, target album does not exist, thus create it
__weak ALAssetsLibrary* weakSelf = self;
//create new assets album
[self addAssetsGroupAlbumWithName:albumName
resultBlock:^(ALAssetsGroup *group) {
//get the photo's instance
[weakSelf assetForURL: assetURL
resultBlock:^(ALAsset *asset) {
//add photo to the newly created album
[group addAsset: asset];
//call the completion block
completionBlock(nil);
} failureBlock: completionBlock];
} failureBlock: completionBlock];
//should be the last iteration anyway, but just in case
return;
}
} failureBlock: completionBlock];
}
@end
// // CaptureSessionManager.h // OverlayWithAVSession // // Created by cdn105 on 05/03/14. // Copyright (c) 2014 CDN Solutions. All rights reserved. //
@interface CaptureSessionManager : NSObject{
}
@property (retain) AVCaptureVideoPreviewLayer previewLayer; @property (retain) AVCaptureSession captureSession; @property (retain) AVCaptureStillImageOutput stillImageOutput; @property (nonatomic, retain) UIImage stillImage;
// // CaptureSessionManager.m // OverlayWithAVSession // // Created by cdn105 on 05/03/14. // Copyright (c) 2014 CDN Solutions. All rights reserved. //
@implementation CaptureSessionManager
@synthesize captureSession; @synthesize previewLayer; @synthesize stillImage; @synthesize stillImageOutput;
(id)init { if ((self = [super init])) { [self setCaptureSession:[[AVCaptureSession alloc] init]];
self.captureSession.sessionPreset = AVCaptureSessionPresetPhoto;
} return self; }
// Find a camera with the specified AVCaptureDevicePosition, returning nil if one is not found
//Responsible for switching between front and back camera -(void)switchCamera { if(captureSession) {
//Indicate that some changes will be made to the session
[captureSession beginConfiguration];
//Remove existing input
AVCaptureInput* currentCameraInput = [captureSession.inputs objectAtIndex:0];
[captureSession removeInput:currentCameraInput];
//Get new input
AVCaptureDevice *newCamera = nil;
if(((AVCaptureDeviceInput*)currentCameraInput).device.position == AVCaptureDevicePositionBack)
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionFront];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
//appDelegate.capturedFromFront=YES;
}
else
{
newCamera = [self cameraWithPosition:AVCaptureDevicePositionBack];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
// appDelegate.capturedFromFront=NO;
}
//Add input to session
AVCaptureDeviceInput *newVideoInput = [[AVCaptureDeviceInput alloc] initWithDevice:newCamera error:nil];
[captureSession addInput:newVideoInput];
//Commit all the configuration changes at once
[captureSession commitConfiguration];
}
}
(AVCaptureStillImageOutput )addStillImageOutput { [self setStillImageOutput:[[AVCaptureStillImageOutput alloc] init]]; NSDictionary outputSettings = [[NSDictionary alloc] initWithObjectsAndKeys:AVVideoCodecJPEG,AVVideoCodecKey,nil]; [[self stillImageOutput] setOutputSettings:outputSettings];
AVCaptureConnection videoConnection = nil; for (AVCaptureConnection connection in [[self stillImageOutput] connections]) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo] ) { videoConnection = connection; break; } } if (videoConnection) { break; } }
[[self captureSession] addOutput:[self stillImageOutput]]; return self.stillImageOutput; }
(void)captureStillImage { AVCaptureConnection videoConnection = nil; for (AVCaptureConnection connection in [[self stillImageOutput] connections]) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo]) { videoConnection = connection; break; } } if (videoConnection) { break; } }
NSLog(@"about to request a capture from: %@", [self stillImageOutput]); [[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError error) { CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); if (exifAttachments) { NSLog(@"attachements: %@", exifAttachments); } else { NSLog(@"no attachments"); } NSData imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData];
NSLog(@" %f : %f", image.size.width, image.size.height);
[self setStillImage:image];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
}];
}
(void)dealloc {
[[self captureSession] stopRunning];
previewLayer = nil; captureSession = nil; stillImageOutput=nil; stillImage=nil;
}
@end
// // ViewController.h // AVCaptureSessionPOC // // Created by jyotisahu on 06/10/15. // Copyright (c) 2015 cdnsol. All rights reserved. //
@interface ViewController : UIViewController @property (retain) CaptureSessionManager *captureManager;
@end
// // ViewController.m // AVCaptureSessionPOC // // Created by jyotisahu on 06/10/15. // Copyright (c) 2015 cdnsol. All rights reserved. //
@interface ViewController () @property (strong, nonatomic) IBOutlet UIButton btnCamera; @property (retain) AVCaptureStillImageOutput stillImageOutput; @property (strong, nonatomic) IBOutlet UIImageView imgFrame; @property (nonatomic, retain) UIImage stillImage; @property (strong, nonatomic) IBOutlet UIView viewFrame; @property (strong, atomic) ALAssetsLibrary library; @end
@implementation ViewController @synthesize captureManager,stillImage,stillImageOutput;
(void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. self.library = [[ALAssetsLibrary alloc] init];
[self setCaptureManager:[[CaptureSessionManager alloc] init]];
[[self captureManager] addVideoInput];
[[self captureManager] addVideoPreviewLayer];
[[[self captureManager]previewLayer] setFrame:CGRectMake(10, 65,380,380)]; [[[self viewFrame] layer] addSublayer:[[self captureManager] previewLayer]]; // [self.view bringSubviewToFront:self.viewFrame];
self.stillImageOutput=[[self captureManager] addStillImageOutput]; [[self.captureManager captureSession] startRunning]; }
-(void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self.imgFrame setImage:[UIImage imageNamed:@"framss.png"]];
}
-(void)resolutionUpdated:(NSNotification*)sender { // CMFormatDescriptionRef formatDescription = [[self captureManager] formatDescription]; //CMVideoDimensions formatDimensions = CMVideoFormatDescriptionGetDimensions(formatDescription); }
(IBAction)doActionCameraBtnPressed:(id)sender {
[[UIApplication sharedApplication] beginIgnoringInteractionEvents];
[self captureImage];
//For Simulator //self.stillImage=[UIImage imageNamed:@"capturedImage.png"]; //[self sendImage]; }
-(void)captureImage { AVCaptureConnection videoConnection = nil; for (AVCaptureConnection connection in [[self stillImageOutput] connections]) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo]) { videoConnection = connection; break; } } if (videoConnection) { break; } }
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) {
CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL);
if (exifAttachments) {
NSLog(@"attachements: %@", exifAttachments);
} else {
NSLog(@"no attachments");
}
NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];
UIImage *image = [[UIImage alloc] initWithData:imageData];
NSLog(@" %f : %f", image.size.width, image.size.height);
self.stillImage=image;
imageData=nil;
image=nil;
[self sendImage];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil];
}];
}
(void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; //[self startCamera]; [UIView setAnimationsEnabled:NO];
[UIView setAnimationsEnabled:YES]; [CATransaction flush]; }
-(void)sendImage { CGSize newSize = CGSizeMake(1080, 1480); UIImage frame=[UIImage imageNamed:@"framss.png"]; UIImage imgCapture=self.stillImage;
UIGraphicsBeginImageContext( newSize );
[imgCapture drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
[frame drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:1.0];
self.stillImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
[self saveImageInLibrary];
}
-(void)saveImageInLibrary { // create an album in phone and save photo in that album [self.library saveImage:self.stillImage toAlbum:@"POC" withCompletionBlock:^(NSError *error) { if (error!=nil) {
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:nil message:@"Something went wrong. Please try again." delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
else {
PhotoViewController *photoView=[self.storyboard instantiateViewControllerWithIdentifier:@"PhotoViewController"];
photoView.imageSaved=self.stillImage;
[self.navigationController pushViewController:photoView animated:YES];
}
}];
} /* //capture image
(void)captureStillImage { AVCaptureConnection videoConnection = nil; for (AVCaptureConnection connection in [[self stillImageOutput] connections]) { for (AVCaptureInputPort *port in [connection inputPorts]) { if ([[port mediaType] isEqual:AVMediaTypeVideo]) { videoConnection = connection; break; } } if (videoConnection) { break; } }
[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError error) { CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer, kCGImagePropertyExifDictionary, NULL); if (exifAttachments) { NSLog(@"attachements: %@", exifAttachments); } else { NSLog(@"no attachments"); } NSData imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer]; UIImage *image = [[UIImage alloc] initWithData:imageData]; NSLog(@" %f : %f", image.size.width, image.size.height);
self.stillImage=image; imageData=nil; image=nil;
[self performSegueWithIdentifier:@"Photo" sender:self];
[[NSNotificationCenter defaultCenter] postNotificationName:kImageCapturedSuccessfully object:nil]; }];
}
//decorate user interface -(void)decorateUI {
if (IS_IPHONE_5) self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgCamera5.png"]]; else self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"bgCamera4.png"]]; self.myScrollView.contentSize=CGSizeMake(960, 428); self.myScrollView.pagingEnabled=YES; self.myScrollView.bounces=NO; self.pageControl.numberOfPages=3; self.pageControl.hidden=YES;
[self setCaptureManager:[[CaptureSessionManager alloc] init]];
[[self captureManager] addVideoInput];
[[self captureManager] addVideoPreviewLayer];
//camera overlay frame if (IS_IPHONE_5) [[[self captureManager]previewLayer] setFrame:CGRectMake(0, 46, 320, 428)]; else [[[self captureManager]previewLayer] setFrame:CGRectMake(0, 0, 320, 428)];
[[[self view] layer] addSublayer:[[self captureManager] previewLayer]]; [self.view bringSubviewToFront:self.myScrollView];
if([self.comeFrm isEqualToString:FRAME]) {
CacheManager cM=[CacheManager sharedCacheManager]; NSString frameName=[[[cM.dataArray objectAtIndex:self.strSection] valueForKey:IMAGE_ARRAY] objectAtIndex:self.strBtn]; self.imgViewFrame1.image=[UIImage imageNamed:frameName]; } else { self.imgViewFrame1.image=[UIImage imageNamed:@"f001.png"]; self.imgViewFrame2.image=[UIImage imageNamed:@"f002.png"]; self.imgViewFrame3.image=[UIImage imageNamed:@"f003.png"];
}
if (IS_IPHONE_5)
{ [self.myScrollView setFrame:CGRectMake(0,46, 320, 428)]; } else { [self.myScrollView setFrame:CGRectMake(0,0, 320, 428)]; }
UIButton btnFrame=(UIButton)[self.view viewWithTag:3]; UIButton btnGallery=(UIButton)[self.view viewWithTag:5]; UIImageView imgViewBottom=(UIImageView )[self.view viewWithTag:2]; UIButton btnInfo=(UIButton)[self.view viewWithTag:6]; UIButton btnSwift=(UIButton)[self.view viewWithTag:1];
[[self view] addSubview:imgViewBottom]; [[self view] addSubview:btnFrame]; [[self view] addSubview:btnGallery]; [[self view] addSubview:btnInfo]; [[self view] addSubview:btnSwift];
[self.view addSubview:self.btnCapture];
self.stillImageOutput=[[self captureManager] addStillImageOutput];
[[self.captureManager captureSession] startRunning]; } */ @end
Archive_11.zip Archive.zip Archive.zip // // ALAssetsLibrary category to handle a custom photo album // // Created by Mayank Jain on 16/09/14. // Copyright (c) 2014 cdnsol.com. All rights reserved. //
import <Foundation/Foundation.h>
import <UIKit/UIKit.h>
import <AssetsLibrary/AssetsLibrary.h>
typedef void(^SaveImageCompletion)(NSError* error);
@interface ALAssetsLibrary(CustomPhotoAlbum)
-(void)saveImage:(UIImage)image toAlbum:(NSString)albumName withCompletionBlock:(SaveImageCompletion)completionBlock; -(void)addAssetURL:(NSURL)assetURL toAlbum:(NSString)albumName withCompletionBlock:(SaveImageCompletion)completionBlock;
@end