ambujshukla / CustomData

0 stars 0 forks source link

Assets #1

Open ambujshukla opened 9 years ago

ambujshukla commented 9 years ago

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

ambujshukla commented 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. //

import "ALAssetsLibrary+CustomPhotoAlbum.h"

/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

ambujshukla commented 9 years ago

// // CaptureSessionManager.h // OverlayWithAVSession // // Created by cdn105 on 05/03/14. // Copyright (c) 2014 CDN Solutions. All rights reserved. //

import <Foundation/Foundation.h>

import <CoreMedia/CoreMedia.h>

import <AVFoundation/AVFoundation.h>

import <ImageIO/ImageIO.h>

define kImageCapturedSuccessfully @"imageCapturedSuccessfully"

@interface CaptureSessionManager : NSObject{

}

@property (retain) AVCaptureVideoPreviewLayer previewLayer; @property (retain) AVCaptureSession captureSession; @property (retain) AVCaptureStillImageOutput stillImageOutput; @property (nonatomic, retain) UIImage stillImage;

ambujshukla commented 9 years ago

// // CaptureSessionManager.m // OverlayWithAVSession // // Created by cdn105 on 05/03/14. // Copyright (c) 2014 CDN Solutions. All rights reserved. //

import "CaptureSessionManager.h"

import <ImageIO/ImageIO.h>

import "AppDelegate.h"

@implementation CaptureSessionManager

@synthesize captureSession; @synthesize previewLayer; @synthesize stillImage; @synthesize stillImageOutput;

pragma mark init

pragma mark Capture Session Configuration

// 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];

}

}

pragma mark dealloc

}

@end

ambujshukla commented 9 years ago

// // ViewController.h // AVCaptureSessionPOC // // Created by jyotisahu on 06/10/15. // Copyright (c) 2015 cdnsol. All rights reserved. //

import <UIKit/UIKit.h>

import <CoreMedia/CoreMedia.h>

import <AVFoundation/AVFoundation.h>

import <ImageIO/ImageIO.h>

import "CaptureSessionManager.h"

@interface ViewController : UIViewController @property (retain) CaptureSessionManager *captureManager;

@end

ambujshukla commented 9 years ago

// // ViewController.m // AVCaptureSessionPOC // // Created by jyotisahu on 06/10/15. // Copyright (c) 2015 cdnsol. All rights reserved. //

import "ViewController.h"

import "UIDevice-Hardware.h"

import "PhotoViewController.h"

import <AssetsLibrary/AssetsLibrary.h>

import "ALAssetsLibrary+CustomPhotoAlbum.h"

@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)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); }

-(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)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