Skip to content

Commit

Permalink
- read images from document directory (iTunes sharing supported)
Browse files Browse the repository at this point in the history
- add video support
- improve messages
  • Loading branch information
itok committed Jul 24, 2011
1 parent 9cd34dd commit 02e7b66
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 11 deletions.
3 changes: 3 additions & 0 deletions AssetMover.xcodeproj/project.pbxproj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@
/* Begin PBXProject section */ /* Begin PBXProject section */
577D2AEA13C104BB00DBBAFD /* Project object */ = { 577D2AEA13C104BB00DBBAFD /* Project object */ = {
isa = PBXProject; isa = PBXProject;
attributes = {
LastUpgradeCheck = 0420;
};
buildConfigurationList = 577D2AED13C104BB00DBBAFD /* Build configuration list for PBXProject "AssetMover" */; buildConfigurationList = 577D2AED13C104BB00DBBAFD /* Build configuration list for PBXProject "AssetMover" */;
compatibilityVersion = "Xcode 3.2"; compatibilityVersion = "Xcode 3.2";
developmentRegion = English; developmentRegion = English;
Expand Down
12 changes: 12 additions & 0 deletions AssetMover/AssetMover-Info.plist
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
<string>en</string> <string>en</string>
<key>CFBundleDisplayName</key> <key>CFBundleDisplayName</key>
<string>${PRODUCT_NAME}</string> <string>${PRODUCT_NAME}</string>
<key>CFBundleDocumentTypes</key>
<array/>
<key>CFBundleExecutable</key> <key>CFBundleExecutable</key>
<string>${EXECUTABLE_NAME}</string> <string>${EXECUTABLE_NAME}</string>
<key>CFBundleIconFile</key> <key>CFBundleIconFile</key>
Expand All @@ -22,17 +24,27 @@
<string>1.0</string> <string>1.0</string>
<key>CFBundleSignature</key> <key>CFBundleSignature</key>
<string>????</string> <string>????</string>
<key>CFBundleURLTypes</key>
<array/>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>1.0</string> <string>1.0</string>
<key>LSRequiresIPhoneOS</key> <key>LSRequiresIPhoneOS</key>
<true/> <true/>
<key>NSMainNibFile</key> <key>NSMainNibFile</key>
<string>MainWindow</string> <string>MainWindow</string>
<key>UIApplicationExitsOnSuspend</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
<key>UISupportedInterfaceOrientations</key> <key>UISupportedInterfaceOrientations</key>
<array> <array>
<string>UIInterfaceOrientationPortrait</string> <string>UIInterfaceOrientationPortrait</string>
<string>UIInterfaceOrientationLandscapeLeft</string> <string>UIInterfaceOrientationLandscapeLeft</string>
<string>UIInterfaceOrientationLandscapeRight</string> <string>UIInterfaceOrientationLandscapeRight</string>
</array> </array>
<key>UTExportedTypeDeclarations</key>
<array/>
<key>UTImportedTypeDeclarations</key>
<array/>
</dict> </dict>
</plist> </plist>
3 changes: 3 additions & 0 deletions AssetMover/AssetMoverAppDelegate.h
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@


UIImageView *_imageView; UIImageView *_imageView;
UILabel *_label; UILabel *_label;

NSMutableArray* paths;
NSUInteger count;
} }


@property (nonatomic, retain) IBOutlet UIWindow *window; @property (nonatomic, retain) IBOutlet UIWindow *window;
Expand Down
75 changes: 67 additions & 8 deletions AssetMover/AssetMoverAppDelegate.m
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -18,29 +18,87 @@ @implementation AssetMoverAppDelegate
@synthesize imageView = _imageView; @synthesize imageView = _imageView;
@synthesize label = _label; @synthesize label = _label;


-(void) prepareForPath:(NSString*)path
{
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
if (UTTypeConformsTo(uti, kUTTypeImage) || (UTTypeConformsTo(uti, kUTTypeMovie) && UIVideoAtPathIsCompatibleWithSavedPhotosAlbum(path))) {
[paths addObject:path];
}
}

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{ {
paths = [[NSMutableArray alloc] init];

// Override point for customization after application launch. // Override point for customization after application launch.
[self.window makeKeyAndVisible]; [self.window makeKeyAndVisible];

NSFileManager* mgr = [[[NSFileManager alloc] init] autorelease];

// resource directory
NSArray* files = [mgr subpathsAtPath:[[NSBundle mainBundle] resourcePath]];
for (NSString* file in files) {
[self prepareForPath:[[NSBundle mainBundle] pathForResource:file ofType:@""]];
}

// document directory
NSString* dir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
files = [mgr subpathsAtPath:dir];
for (NSString* file in files) {
[self prepareForPath:[dir stringByAppendingPathComponent:file]];
}

if ([paths count] == 0) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"No images or videos" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
return YES;
}

self.label.text = [NSString stringWithFormat:@"%d/%d", 0, [paths count]];

ALAssetsLibrary* library = [[[ALAssetsLibrary alloc] init] autorelease];


dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void) {
NSFileManager* mgr = [[[NSFileManager alloc] init] autorelease]; for (int i = 0; i < [paths count]; i++) {
NSArray* files = [mgr subpathsAtPath:[[NSBundle mainBundle] resourcePath]]; NSString* path = [paths objectAtIndex:i];
__block int cnt = 0;
for (NSString* file in files) { CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[path pathExtension], NULL);
CFStringRef uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, (CFStringRef)[file pathExtension], NULL);
if (UTTypeConformsTo(uti, kUTTypeImage)) { if (UTTypeConformsTo(uti, kUTTypeImage)) {
NSData* d = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:file ofType:@""]]; NSData* d = [NSData dataWithContentsOfFile:path];


dispatch_sync(dispatch_get_main_queue(), ^(void) { dispatch_sync(dispatch_get_main_queue(), ^(void) {
self.imageView.image = [UIImage imageWithData:d]; self.imageView.image = [UIImage imageWithData:d];
self.label.text = [NSString stringWithFormat:@"%d", ++cnt]; self.label.text = [NSString stringWithFormat:@"%d/%d", i + 1, [paths count]];
}); });


[[[[ALAssetsLibrary alloc] init] autorelease] writeImageDataToSavedPhotosAlbum:d metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) { [library writeImageDataToSavedPhotosAlbum:d metadata:nil completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"%@ (%@)", [error localizedDescription], [path lastPathComponent]);
}
}]; }];
} else if (UTTypeConformsTo(uti, kUTTypeMovie)) {
dispatch_sync(dispatch_get_main_queue(), ^(void) {
self.imageView.image = nil;
self.label.text = [NSString stringWithFormat:@"%d/%d", i + 1, [paths count]];
});
[library writeVideoAtPathToSavedPhotosAlbum:[NSURL fileURLWithPath:path] completionBlock:^(NSURL *assetURL, NSError *error) {
if (error) {
NSLog(@"%@ (%@)", [error localizedDescription], [path lastPathComponent]);
}
}];
}

if ([path hasPrefix:dir]) {
[mgr removeItemAtPath:path error:nil];
} }
} }

dispatch_async(dispatch_get_main_queue(), ^(void) {
UIAlertView* alert = [[UIAlertView alloc] initWithTitle:@"Completed" message:@"" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[alert release];
});
}); });


return YES; return YES;
Expand Down Expand Up @@ -87,6 +145,7 @@ - (void)applicationWillTerminate:(UIApplication *)application


- (void)dealloc - (void)dealloc
{ {
[paths release];
[_window release]; [_window release];
[_imageView release]; [_imageView release];
[_label release]; [_label release];
Expand Down
64 changes: 61 additions & 3 deletions AssetMover/en.lproj/MainWindow.xib
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
<int key="NSvFlags">1298</int> <int key="NSvFlags">1298</int>
<string key="NSFrame">{{40, 40}, {240, 240}}</string> <string key="NSFrame">{{40, 40}, {240, 240}}</string>
<reference key="NSSuperview" ref="380026005"/> <reference key="NSSuperview" ref="380026005"/>
<reference key="NSNextKeyView"/> <reference key="NSWindow"/>
<int key="IBUIContentMode">1</int> <int key="IBUIContentMode">1</int>
<bool key="IBUIUserInteractionEnabled">NO</bool> <bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string> <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
Expand All @@ -62,12 +62,18 @@
<int key="NSvFlags">1316</int> <int key="NSvFlags">1316</int>
<string key="NSFrame">{{40, 288}, {240, 21}}</string> <string key="NSFrame">{{40, 288}, {240, 21}}</string>
<reference key="NSSuperview" ref="380026005"/> <reference key="NSSuperview" ref="380026005"/>
<reference key="NSWindow"/>
<bool key="IBUIOpaque">NO</bool> <bool key="IBUIOpaque">NO</bool>
<bool key="IBUIClipsSubviews">YES</bool> <bool key="IBUIClipsSubviews">YES</bool>
<int key="IBUIContentMode">7</int> <int key="IBUIContentMode">7</int>
<bool key="IBUIUserInteractionEnabled">NO</bool> <bool key="IBUIUserInteractionEnabled">NO</bool>
<string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string> <string key="targetRuntimeIdentifier">IBCocoaTouchFramework</string>
<string key="IBUIText">Label</string> <string key="IBUIText"/>
<object class="NSFont" key="IBUIFont">
<string key="NSName">Helvetica</string>
<double key="NSSize">17</double>
<int key="NSfFlags">16</int>
</object>
<object class="NSColor" key="IBUITextColor"> <object class="NSColor" key="IBUITextColor">
<int key="NSColorSpace">1</int> <int key="NSColorSpace">1</int>
<bytes key="NSRGB">MCAwIDAAA</bytes> <bytes key="NSRGB">MCAwIDAAA</bytes>
Expand All @@ -81,6 +87,7 @@
<object class="NSPSMatrix" key="NSFrameMatrix"/> <object class="NSPSMatrix" key="NSFrameMatrix"/>
<string key="NSFrameSize">{320, 480}</string> <string key="NSFrameSize">{320, 480}</string>
<reference key="NSSuperview"/> <reference key="NSSuperview"/>
<reference key="NSWindow"/>
<reference key="NSNextKeyView" ref="145541797"/> <reference key="NSNextKeyView" ref="145541797"/>
<object class="NSColor" key="IBUIBackgroundColor"> <object class="NSColor" key="IBUIBackgroundColor">
<int key="NSColorSpace">1</int> <int key="NSColorSpace">1</int>
Expand Down Expand Up @@ -221,7 +228,58 @@
<nil key="sourceID"/> <nil key="sourceID"/>
<int key="maxID">13</int> <int key="maxID">13</int>
</object> </object>
<object class="IBClassDescriber" key="IBDocument.Classes"/> <object class="IBClassDescriber" key="IBDocument.Classes">
<object class="NSMutableArray" key="referencedPartialClassDescriptions">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBPartialClassDescription">
<string key="className">AssetMoverAppDelegate</string>
<string key="superclassName">NSObject</string>
<object class="NSMutableDictionary" key="outlets">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>imageView</string>
<string>label</string>
<string>window</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>UIImageView</string>
<string>UILabel</string>
<string>UIWindow</string>
</object>
</object>
<object class="NSMutableDictionary" key="toOneOutletInfosByName">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="NSArray" key="dict.sortedKeys">
<bool key="EncodedWithXMLCoder">YES</bool>
<string>imageView</string>
<string>label</string>
<string>window</string>
</object>
<object class="NSMutableArray" key="dict.values">
<bool key="EncodedWithXMLCoder">YES</bool>
<object class="IBToOneOutletInfo">
<string key="name">imageView</string>
<string key="candidateClassName">UIImageView</string>
</object>
<object class="IBToOneOutletInfo">
<string key="name">label</string>
<string key="candidateClassName">UILabel</string>
</object>
<object class="IBToOneOutletInfo">
<string key="name">window</string>
<string key="candidateClassName">UIWindow</string>
</object>
</object>
</object>
<object class="IBClassDescriptionSource" key="sourceIdentifier">
<string key="majorKey">IBProjectSource</string>
<string key="minorKey">./Classes/AssetMoverAppDelegate.h</string>
</object>
</object>
</object>
</object>
<int key="IBDocument.localizationMode">0</int> <int key="IBDocument.localizationMode">0</int>
<string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string> <string key="IBDocument.TargetRuntimeIdentifier">IBCocoaTouchFramework</string>
<object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies"> <object class="NSMutableDictionary" key="IBDocument.PluginDeclaredDevelopmentDependencies">
Expand Down

0 comments on commit 02e7b66

Please sign in to comment.